title: socket.io app fails when many clients are connected
body: <p>I coded a feature for one of my sites that would push an alert to a user when another user sends them a message. The feature works in development and in production if I only enable it for a few users. The feature then fails miserably (the push alert stops being sent) when enabled for a few thousand concurrent users (the server stays up so it's not simply dying). From the logs it seems that the socket isn't being found in the 'send_msg' event.</p>
  
  <p>I also noticed that when I try to access <a href="http://www.mydomain.com:4086/socket.io/socket.io.js" rel="nofollow">http://www.mydomain.com:4086/socket.io/socket.io.js</a> directly from the browser, sometimes it loads and sometimes it doesn't and I have no idea what the cause may be. Does anything look wrong in the code I posted below? I've been bashing my head over this for the last three days.</p>
  
  <p><strong>Edit:</strong> It looks like raising the ulimit from 1024 to 65535 buys the application a few more minutes before it starts failing with "warn  - client not handshaken client should reconnect" and "warn  - websocket parser error: no handler for opcode 10" errors again. I'm sure I don't have anywhere near 65535 concurrent connections at one time. Can someone explain if I'm on the right track?</p>
  
  <p><strong>Server code (server.js):</strong></p>
  
  <pre><code>var io = require('socket.io').listen(4086);
  var clients = {};
  
  io.sockets.on('connection', function (socket) {
      socket.on('login', function (data) {
          if (typeof data.id_user !== 'undefined' &amp;&amp; data.id_user &gt; 0) {
              socket.set('id_user', data.id_user, function() {});
              clients[data.id_user] = socket.id;
          }
      });
  
      socket.on('send_msg', function (data) {
          if (typeof io.sockets.sockets[clients[data.id_sendee]] !== 'undefined') {
              var sendeeSocket = io.sockets.sockets[clients[data.id_sendee]];
              sendeeSocket.emit('newalert', {
                  sender_profile: data.sender_profile,
                  msg: data.msg,
                  send_to: data.send_to });
          }
      });
  
      socket.on('disconnect', function() {
          socket.get('id_user', function (err, id_user) {
              if (typeof id_user !== 'undefined' &amp;&amp; id_user &gt; 0) {
                  delete clients[id_user];
              }
          });
      });
  });
  </code></pre>
  
  <p><strong>Client code:</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="http://www.mydomain.com:4086/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var socket = io.connect('http://www.mydomain.com:4086');
  
      socket.emit('login', { id_user: &lt;?=$idUser?&gt; });
  
      socket.on('newalert', function (data) {
          $("#msg_stream").hide();
          $("#stream_sender_profile").html(data.sender_profile);
          $("#stream_msg").text(data.msg);
          $("#stream_send_to").html(data.send_to);
          $("#msg_stream").fadeIn();
      });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  .......
  
  &lt;script&gt;
      // Alert stream
      $(document).ready(function() {
          $("#submit_btn").click(function() {
              var msg = $("#message").val();
              if (msg) {
                  if (msg.length &gt; 255) {
                      msg = msg.substr(0, 255) + '...';
                  }
                  socket.emit('send_msg', {
                      sender_profile: '&lt;a href="/profile/view/&lt;?=$idUser?&gt;" class="stream_dismiss"&gt;&lt;?=$username?&gt;&lt;/a&gt;',
                      id_sendee: &lt;?=$idSendee?&gt;,
                      msg: msg,
                      send_to: '&lt;?=isset($streamSendTo) ? $streamSendTo : '';?&gt;' });
              }
          });
      });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>