title: NodeJS buffer incomplete TCP stream data
body: <p>I am trying to identify an issue with my TCP JSON stream on my live server. What I have found is that if data streamed to me via TCP (in JSON format) is too large, then it does not consistently go through for parsing. I have to stream it a few times to be successful. </p>
  
  <p>The code I am using goes as follows:</p>
  
  <pre><code>socket.once('data', function(data){
      let chunk = "";
      chunk += data.toString(); // Add string on the end of the variable 'chunk'
      let d_index = chunk.indexOf(';'); // Find the delimiter
      // While loop to keep going until no delimiter can be found
      while (d_index &gt; -1) {         
          try {
              let string = chunk.substring(0,d_index); // Create string up until the delimiter
              let json = JSON.parse(string); // Parse the current string
              pages.addJSON(string);
              console.log(json.pagename); // Function that does something with the current chunk of valid json.    
  
          }
          catch(e){
              console.log(e);
          }
          chunk = chunk.substring(d_index+1); // Cuts off the processed chunk
          d_index = chunk.indexOf(';'); // Find the new delimiter
      }      
  });
  </code></pre>
  
  <p>My data is streamed to me as a JSON file, with a <code>;</code> to separate each stream. So for example, <code>{"page": "something"};</code></p>
  
  <p>I am using code from <a href="https://stackoverflow.com/questions/12872563/issues-when-reading-a-string-from-tcp-socket-in-node-js">this question</a> and one of the responses warns that we should buffer whatever code is not complete. I am wondering how to go about doing so, as I believe that my problem may stem from this issue. </p>
  
  <p>Since my JSON stream is somewhat large, I think that a lot of the data does not pass through on connection, then gets cleared by my <code>chunk</code> variable. </p>
  
  <p>The user stated that one possibility was to capture the data via byte size. Unfortunately, this option is not available to me because I do not know how large my streams are going to be. </p>
  
  <p>I have used <code>;</code> to capture the data's endpoint, and that's as far as I know what to do. </p>