I'm setting up a node.js twitter streaming app on heroku. It works perfectly until a second client makes a connection. This is something I wasn't expecting until I showed it to someone :(
I am using tweet-pipe for the Twitter Stram API functionality: https://github.com/peeinears/tweet-pipe
To get Socket.IO working on Heroku I followed these instructions: https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
In my app.js file I have
io.sockets.on('connection', function (socket) {
tp.stream('statuses/filter', params, false, function (stream) {
stream.on('tweet', function (tweet) {
socket.emit('tweet', tweet );
});
});
});
Again, this all works great until another client visits the page. Then the original connection is lost while the second client has a connection. Any thoughts? I'm a little new to the Socket.IO stuff.
AFAIK you can only have one active stream per twitter handle. You cannot use tp.stream for second client.
Sources:
Here's how I fixed it, thanks to Chad in the comments. It seems to work fine now as it's live and somewhat healthy.
io.sockets.on('connection', function (socket) {
var tp = new TweetPipe({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
token: process.env.TOKEN,
token_secret: process.env.TOKEN_SECRET
});
tp.stream('statuses/filter', params, false, function (stream) {
stream.on('tweet', function (tweet) {
io.sockets.emit('tweet', tweet );
});
});
});