title: Multi-threaded socket.io in single process in node.js application
body: <p>I've been digging around trying to see if anyone else has run into this, but I sort of expected not and that's what I found.</p>
  
  <p>The problem I'm facing right now is related to the architecture of the application I'm building; a game server in node.js. As of right now, the server starts up with a single "manager" process which has control over all the child processes. Each "zone" or "area" of the game is spun up using <code>cluster.fork()</code> using a separate process so that each "zone" can have its own thread on the server to optimize the ability for the server to run many of these "zones" at once.</p>
  
  <p>The dilemma I'm facing right now is this. Since each "zone" is running in a single thread, and that thread is already going to be running complex 3D math X times per second to keep the "zone" in sync, I don't want to open Socket.io (the communication library used for talking with the game client) in that same thread. I guess where I'm hung up is on the performance of using the <code>cluster.on('message')</code> handler in the master process and piping messages from Socket.io into the master process using <code>process.send({...})</code>.</p>
  
  <p>I'm trying to see if anyone else has experience with this or knows on a fundamental level how that whole cluster module works and essentially this:</p>
  
  <p>Is it faster to call:</p>
  
  <pre><code>var io = require('socket.io').listen( 1337 );
  io.sockets.on('connection',...); //map all the functionality here in the "zone" thread
  </code></pre>
  
  <p>Or to do something more like this:</p>
  
  <pre><code>var cluster = require('cluster');
  var io = [];
  //arbitrary number for the thread count, not important for theory testing
  for( var i = 0; i &lt; 4; ++i) {
      var process = cluster.fork({...});
      process.on('message',myRelayFunction);
      io.push( process );
  }
  </code></pre>
  
  <p>Assuming of course that the <code>cluster.fork()</code> call was piping into a script that did nothing but open a socket.io connection and relay the messages from the various clients into the "zone" process.</p>
  
  <p>I've tried running performance tests on this, but I'm finding it very hard to tell if this is faster or not since I'm having difficulty saturating the machine with connections to get the output to test with.</p>
  
  <p>I want to make sure this is scalable so that under a heavier load, the application can make the best use of multiple CPU cores on a server. So that of course says I should multi-thread the socket.io listeners into several threads, but my concern is that since they're all technically piping back into a single threaded process, wouldn't that cost me any possible performance increase of multi-threading them? I'm kind of coming up empty in the testing/benchmarking department here and I don't know the theory behind how all this works well enough to say what's the best fundamental approach here... so I would very much appreciate any feedback/input anyone has on this ;)</p>