How to disable multiplexing using Socket.io

I use Socket.io to send live tweets to my users using the Twitter Streaming API (my implementation is more or less based on this guide ).

The problem is that every time a connection event is triggered by Socket.io, the newly connected client forces all other clients connected to the server to stop updating. Although it will take too much time to go through all the hacking that I tried, I will say that I played with it enough that I think the problem is with multiplexing Socket.io connections from several clients (enabled by default) as an enhancement performance to allow multiple clients or connections to share the same underlying socket. In short, I believe that this is so because I do not think that new connections can affect old connections in this way, if not for multiplexing the connection. In other words, if a new independent connection with its own kernel (TCP) was created each time the client connected, this would be impossible, since one connection does not know anything about the other and therefore cannot affect other client states as it happens currently. It also makes me think that simply disabling the multiplexing function will be the easiest way to get around this problem, since I am not interested in scaling, because Node.js already handles all concurrency, I will probably have to handle it very adequately.

I went through the Socket.io documentation , but could not see where the ability to "demultiplex" connections is opened through the API, so if anyone knows how to do this, I would appreciate your answer.

My code below is pretty standard and simple. But in order to be clear, the problem is that whenever a new client connects to Socket.io, every other client stops receiving new tweets, and updates will no longer be redirected to an older client if I do not update the browser, and in this case, a new updated client will start updating again and receiving fresh tweets, but the remaining clients that are still connected will stop updating.

Server Side Code:

// Code also uses ntwitter (https://github.com/AvianFlu/ntwitter) as an abstraction over Twitter Streaming API io.sockets.on('connection', function (socket) { tweet.stream('statuses/filter', { track : 'new orleans' }, function (stream) { stream.on('data', function (data) { // The following lines simply pre-process data sent from Twitter so junk isn't // unnecessarily sent to the client. if (data.user) { tweets = { text : data.text, image : data.user.profile_image_url, user : data.user.name }; var t = JSON.stringify(tweets); console.log(t); socket.send(t); } }); }); }); 

Client code

 // Notice the option that I passed in as the second argument. This supposedly forces every // new client to create a new connection with the server but it either doesn't work or I'm // implementing it incorrectly. It is the very last configuration option listed in the // documentation linked to above. var socket = io.connect('http://' + location.host, {'force new connection' : true }); socket.on('message', function (tweet) { var t = JSON.parse(tweet); if (t.image) { $('.hero-unit').prepend('<div class="media"><a class="pull-left" href="#"><img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="' + t.image + '"></a><div class="media-body"><h4 class="media-heading">' + t.user + '</h4>' + t.text + '</div></div>'); } }); 

If I think about it wrong or something is wrong with my code, I am definitely open to any suggestions. I will also be happy to answer any additional details. Thanks, advanced!

+4
source share
1 answer

I would try something like this

Serverside:

 io.sockets.on('connection', function (socket) { //Other Connectiony goodness here. }); }); tweet.stream('statuses/filter', { track : 'new orleans' }, function (stream) { stream.on('data', function (data) { // The following lines simply pre-process data sent from Twitter so junk isn't // unnecessarily sent to the client. if (data.user) { tweets = { text : data.text, image : data.user.profile_image_url, user : data.user.name }; var t = JSON.stringify(tweets); console.log(t); io.sockets.emit("tweet", t); } }); 

on the client side:

 var socket = io.connect('http://' + location.host, {'force new connection' : true }); socket.on('tweet', function (tweet) { var t = JSON.parse(tweet); if (t.image) { $('.hero-unit').prepend('<div class="media"><a class="pull-left" href="#"><img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="' + t.image + '"></a><div class="media-body"><h4 class="media-heading">' + t.user + '</h4>' + t.text + '</div></div>'); } }); 

Basically there is a stream from twitter outside your socket, and then in a new tweet emit message for everyone connected.

0
source

All Articles