title: How to use Node.js cluster with ZeroMQ reply workers
body: <p>The basic question is "how to setup a ZeroMQ REPLY socket as a worker?". </p>
  
  <p>In effect, how to swap out the <a href="http://nodejs.org/api/cluster.html#cluster_cluster" rel="nofollow">default example using an HTTP server</a> with a ZeroMQ REPLY server, eg.:</p>
  
  <pre><code>var cluster = require('cluster'),
      zmq = require('zmq');
  
  if (cluster.isMaster) {
    cluster.fork();
    cluster.fork();
  }
  else {
    // Using `http.createServer(..).listen(5555);` works perfectly
  
    // However, the following does not:
    var socket = zmq.socket('rep');
    socket.bind( "tcp://*:5555", function (err) { console.log(process.pid, err); } );
  }
  </code></pre>
  
  <p>The first worker logs 'undefined' (ie. no error), whereas the second worker logs an error: <code>Address already in use</code>.</p>
  
  <p>From the "<a href="http://nodejs.org/api/cluster.html#cluster_how_it_works" rel="nofollow">How it works</a>" docs, the bit that seems appropriate is here (emphasis added):</p>
  
  <blockquote>
    <p>When you call <code>server.listen(...)</code> in a worker, it serializes the arguments and passes the request to the master process. If the master process <strong>already has a listening server matching the worker's requirements, then it passes the handle to the worker</strong>. If it does not already have a listening server matching that requirement, then it will create one, and pass the handle to the worker.</p>
  </blockquote>
  
  <p>How to "match requirements" with a ZeroMQ REPLY socket?</p>