title: Synchronous TCP Read in Node.js
body: <p>Is there a way to do a synchronous read of a TCP socket in node.js?</p>
  
  <p>I'm well aware of how to do it asynchronously by adding a callback to the socket's 'data' event:</p>
  
  <pre><code>socket.on('data', function(data) {
      // now we have the string data to do whatever with
  });
  </code></pre>
  
  <p>I'm also aware that trying to block with a function call instead of registering callbacks goes against node's design, but we are trying to update an old node module that acts as a client for my university while maintaining backwards compatibility. So we currently have:</p>
  
  <pre><code>var someData = ourModule.getData();
  </code></pre>
  
  <p>Where <code>getData()</code> previously had a bunch of logic behind it, but now we just want to send to the server "run getData()" and wait for the result. That way all logic is server side, and not duplicated client and server side. This module already maintains a TCP connection to the server so we are just piggybacking on that.</p>
  
  <p>Here are the solutions I've tried:</p>
  
  <ol>
  <li><p>Find a blocking read function for the socket hidden somewhere similar to python's socket library within node's net module.</p>
  
  <pre><code>string_from_tcp = socket.recv(1024)
  </code></pre>
  
  <p>The problem here is that it doesn't seem to exist (unsurprisingly because it goes against node's ideology).</p></li>
  <li><p>This <a href="https://www.npmjs.com/package/syncnet" rel="noreferrer">syncnet module</a> adds what I need, but has no Windows support; so I'd have to add that.</p></li>
  <li><p>Find a function that allow's node to unblock the event loop, then return back, such that this works:</p>
  
  <pre><code>var theData = null;
  clientSocket.on('data', function(data) {
      theData = data;
  });
  
  clientSocket.write("we want some data");
  
  while(theData === null) {
      someNodeFunctionThatUnblocksEventLoopThenReturnsHere(); // in this function node can check the tcp socket and call the above 'data' callback, thus changing the value of theData
  }
  
  // now theData should be something!
  </code></pre>
  
  <p>Obvious problem here is that I don't think such a thing exists.</p></li>
  <li><p>Use ECMAScript 6 generator functions:</p>
  
  <pre><code>var stringFromTcp = yield socketRead(1024);
  </code></pre>
  
  <p>The problem here is that we'd be forcing students to update their JavaScript clients to this new syntax and understanding ES6 is outside the scopes of the courses that use this.</p></li>
  <li><p>Use node-gyp and add to our node module an interface to a C++ TCP library that does support synchronous reads such as boost's asio. This would probably work but getting the node module to compile with boost cross platform has been a huge pain. So I've come to Stack Overflow to make sure I'm not over-complicating this problem.</p></li>
  </ol>
  
  <p>In the simplest terms I'm just trying to create a command line JavaScript program that supports synchronous tcp reads.</p>
  
  <p>So any other ideas? And sorry in advance if this seems blasphemous in context of a node project, and thanks for any input.</p>