2

I am trying to stream tweets to the client and everything works fine, as long as there is only one connection. If I am am streaming tweets in one tab of my browser and open the page up in a new tab, the first tab will stop receiving the new tweets and only the second tab will receive them.

Server code.

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? socket.emit('twitter', data) : ''
    });
 });

Client code:

var socket = io.connect()

socket.on('twitter', function (data) {

    //Do really awesome things.

});

What am I doing wrong? It has to be simple. I can't imagine its this hard to support multiple connections.

Cheers

Andy Thornton
  • 628
  • 5
  • 11
  • 1
    Unless you want each user to stream different tweets, it would probably be wiser not to do this in the connection handler, but instead set it up once and for all and do `io.sockets.emit` -- that should emit the messages to all currently connected sockets. In the connection handler, you would typically set up stuff that's unique to each socket. – Linus Thiel Nov 12 '12 at 16:11

1 Answers1

1

Try changing socket.emit to sockets.emit:

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? sockets.emit('twitter', data) : ''
    });
 });

Or if that doesn't work, adding socket.broadcast.emit in addition to socket.emit as shown below:

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? socket.emit('twitter', data) : ''
         data.geo  ? socket.broadcast.emit('twitter', data) : ''
    });
 });
Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • I did that but when I open a new tab and the new page doesn't work but all the previous tabs do. Like the new tab spawns the broadcast to all the other tabs at the cost of it not updating itself. – Andy Thornton Nov 12 '12 at 01:47
  • 1
    Well yes, `broadcast.emit` doesn't send it to the socket itself, just to all others. However, you could add an additional line of just `socket.emit` as well, which should fix that issue). – Dylan Cross Nov 12 '12 at 01:51
  • OK, that makes sense. That works. Is that pretty standard way to handle multiple connections or do I just have a fundamental misunderstanding of websockets and multiple connections? – Andy Thornton Nov 12 '12 at 01:54
  • Well, I actually haven't done too much with socket.io recently so a lot of what I learned needs a little refreshing when I get back to work with it, however, I have always had to use `broadcast` with `emit`, and `emit` it as well, so it seems like this is how Socket.IO works. I don't really have a clear understanding with sockets, so I may be off as well. – Dylan Cross Nov 12 '12 at 02:02
  • Thanks for your help, the documentation sure didn't provide much assistance. Many thanks! – Andy Thornton Nov 12 '12 at 02:03
  • 1
    You're looking for `io.sockets.emit`; see [this StackOverflow question](http://stackoverflow.com/questions/10342681/whats-the-difference-between-io-sockets-emit-and-broadcast). – Michelle Tilley Nov 12 '12 at 04:00
  • @BrandonTilley I couldn't remember whether or not `socket` was just a variable that you could set to whatever you wanted. However, that would make sense, so I have updated my answer accordingly. – Dylan Cross Nov 12 '12 at 04:09
  • Don't forget to call `sockets` on `io` (or whatever variable you assign `require('socket.io').listen()` to), as `sockets` by itself is not defined. – Michelle Tilley Nov 12 '12 at 04:27