title: node - send large JSON over net socket
body: <p>The problem is that sending large serialized JSON (over 16,000 characters) over a net socket gets split into chunks. Each chunk fires the <code>data</code> event on the receiving end. So simply running <code>JSON.parse()</code> on the incoming data may fail with <code>SyntaxError: Unexpected end of input</code>.</p>
  
  <p>The work around I've managed to come up with so far is to append a null character (<code>'\u0000'</code>) to the end of the serialized JSON, and check for that on the receiving end. Here is an example:</p>
  
  <pre class="lang-js prettyprint-override"><code>var partialData = '';
  client.on( 'data', function( data ) {
      data = data.toString();
      if ( data.charCodeAt( data.length - 1 ) !== 0 ) {
          partialData += data;
          // if data is incomplete then no need to proceed
          return;
      } else {
          // append all but the null character to the existing partial data
          partialData += data.substr( 0, data.length - 1 );
      }
      // pass parsed data to some function for processing
      workWithData( JSON.parse( partialData ));
      // reset partialData for next data transfer
      partialData = '';
  });
  </code></pre>
  
  <p>One of the failures of this model is if the receiver is connected to multiple sockets, and each socket is sending large JSON files.</p>
  
  <p>The reason I'm doing this is because I need to pass data between two processes running on the same box, and I prefer not to use a port. Hence using a net socket. So there would be two questions: First, is there a better way to quickly pass large JSON data between two Node.js processes? Second, if this is the best way then how can I better handle the case where the serialized JSON is being split into chunks when sent?</p>