title: How can zmq (zeromq) be used to send multiple requests and route async responses between two NodeJS servers?
body: <p>I have a NodeJS API web server (let's call it WS1) that receives RESTful HTTP requests from clients, and to respond needs to first query another local server (let's call it WS2).</p>
  
  <p>The flow is pretty much like this:</p>
  
  <ol>
  <li>WS1 receives an HTTP request from a client and parses it.</li>
  <li>WS1 sends a request to WS2 for some information.</li>
  <li>When WS1 receives the response from WS2, it finishes processing the original request and sends a response back to the client.</li>
  </ol>
  
  <p>Until now all communication between WS1 and WS2 has been done through HTTP requests, since the two machines are on the same local network.</p>
  
  <p>To speed things up though I'm considering to start using <a href="http://zeromq.org/" rel="nofollow"><strong>zmq</strong></a> instead. I've looked at the patterns they show on the docs, but still haven't figured out a concurrency problem.</p>
  
  <p>WS1 can send many requests per second to WS2, and there's no guarantee that WS2 replies in the same order as it receives the requests, since some async operations can internally take longer than others.</p>
  
  <p>So, using zmq with NodeJS, how do I make sure that when WS1 receives a message from WS2 it knows to what original client request it belongs to? Is there a built-in mechanism to take care of it?</p>
  
  <p>Thanks!</p>