0

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.

Tyler
  • 829
  • 11
  • 26
  • 2
    Create a new tweet-pipe for each connector, or register the tp stream seperately and broadcast messages. (like `io.sockets.emit('tweet', tweet)`) are some quick ideas. I don't know enough about tweet-pipe or your use case to advise further than that. – Chad Mar 26 '13 at 21:59
  • Thanks @Chad. This helped quite a bit. – Tyler Mar 27 '13 at 18:26

2 Answers2

0

AFAIK you can only have one active stream per twitter handle. You cannot use tp.stream for second client.

Sources:

  1. https://dev.twitter.com/docs/streaming-apis/streams/public
  2. https://dev.twitter.com/discussions/921
user568109
  • 47,225
  • 17
  • 99
  • 123
0

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 );
    });
  });

});
Tyler
  • 829
  • 11
  • 26
  • 1
    Only problem is this will run each time someone connects so you will get duplicated messages to the browser based on the number of people connected. Only run this code once, outside of the 'connection' event, or make sure it only runs once for the first connection. – Chad Mar 27 '13 at 20:46