title: How to disable Multiplexing with Socket.io
body: <p>I am using Socket.io to stream live tweets to my users using Twitter's Streaming API (my implementation is more or less based on <a href="http://www.i-avington.com/Posts/Post/using-nodejs-and-socketio-to-stream-tweets-from-the-twitter-api" rel="nofollow noreferrer">this tutorial</a>). </p>
  
  <p>The problem is that every time a connection event is fired by Socket.io the newly connected client causes every other client connected to the server to cease updating. While it would take too long to go through all the hacks that I tried, I will say that I played with it enough that I believe the problem is caused by Socket.io's multiplexing of the connections from multiple clients (enabled by default) as a performance boost to allow multiple clients or connections to share the same underlying socket. In short, I believe this to be the case because I don't think it would be possible for new connections to affect older connections in this manner if not for the connection multiplexing. In other words, if a new, independent connection with its own underlying (TCP) socket were created every time a client connected it would be impossible for this to occur since one connection would know nothing about the other and therefore couldn't affect any other client's state as is currently happening. This also leads me to believe that simply disabling the multiplexing functionality would be the simplest way to get around this problem since I am not concerned about scaling because Node.js already handles all the concurrency I'm likely to need to handle very adequately. </p>
  
  <p>I have gone through Socket.io's <a href="https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO" rel="nofollow noreferrer">documentation</a> but could not see where the ability to "demultiplex" the connections is exposed via the API, so if anyone knows how to do this I'd create appreciate your response. </p>
  
  <p>My code below is pretty standard and simple. But just to be clear, the issue is that whenever a new client connects to Socket.io every other client stops receiving new tweets and updates are no longer pushed to the older client unless I refresh the browser in which case the newly refreshed client will begin to update and receive fresh tweets again, but the other still connected clients will then stop updating. </p>
  
  <p><strong>Server-side Code:</strong></p>
  
  <pre><code>// Code also uses ntwitter (https://github.com/AvianFlu/ntwitter) as an abstraction over Twitter's 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);
        }  
      });
    });
  });
  </code></pre>
  
  <p><strong>Client-Side Code</strong></p>
  
  <pre><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('&lt;div class="media"&gt;&lt;a class="pull-left" href="#"&gt;&lt;img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="' + t.image + '"&gt;&lt;/a&gt;&lt;div class="media-body"&gt;&lt;h4 class="media-heading"&gt;' + t.user + '&lt;/h4&gt;' + t.text + '&lt;/div&gt;&lt;/div&gt;');
    } 
  });
  </code></pre>
  
  <p>If I am thinking of this incorrectly or if there's something wrong with my code I am definitely open to any suggestions. I'd also be happy to reply with any additional details.</p>