title: Node.js net library: getting complete data from 'data' event
body: <p>I've searched around, and either can't find the exact question I'm trying to answer, or I need someone to explain it to me like I'm 5.</p>
  
  <p>Basically, I have a Node.js script using the Net library.  I'm connecting to multiple hosts, and sending commands, and listening for return data.</p>
  
  <pre><code>var net = require('net');
  
  var nodes = [
      'HOST1,192.168.179.8',
      'HOST2,192.168.179.9',
      'HOST3,192.168.179.10',
      'HOST4,192.168.179.11'
  ];
  
  function connectToServer(tid, ip) {
      var conn = net.createConnection(23, ip);
      conn.on('connect', function() {
          conn.write (login_string);  // login string hidden in pretend variable
      });
      conn.on('data', function(data) {
          var read = data.toString();
          if (read.match(/Login Successful/)) {
              console.log ("Connected to " + ip);
              conn.write(command_string);
          }
  
          else if (read.match(/Command OK/)) { // command_string returned successful,
                                                      // read until /\r\nEND\r\n/
  
                      // First part of data comes in here
              console.log("Got a response from  " + ip + ':' + read);
          }
          else {
                   //rest of data comes in here
               console.log("Atonomous message from " + ip + ':' + read);
          }
      });
      conn.on('end', function() {
          console.log("Lost conncection to " + ip + "!!");
      });
      conn.on('error', function(err) {
          console.log("Connection error: " + err + " for ip " + ip);
      });
  }
  
  nodes.forEach(function(node) {
      var nodeinfo = node.split(",");
      connectToServer(nodeinfo[0], nodeinfo[1]);
  });
  </code></pre>
  
  <p>The data ends up being split into two chunks.  Even if I store the data in a hash and append the first part to the remainder when I read the /\r\nEND\r\n/ delimiter, there's a chunk missing out of the middle.  How do I properly buffer the data in order to make sure I get the complete message from the stream?</p>
  
  <p>EDIT:  Ok, this seems to be working better:</p>
  
  <pre><code>function connectToServer(tid, ip) {
          var conn = net.createConnection(23, ip);
  
          var completeData = '';
  
          conn.on('connect', function() {
                  conn.write (login_string);
          });
          conn.on('data', function(data) {
                  var read = data.toString();
  
                  if (read.match(/Login Successful/)) {
                          console.log ("Connected to " + ip);
  
                          conn.write(command_string);
                  }
                  else {
                          completeData += read;
                  }
  
                  if (completeData.match(/Command OK/)) {
                          if (completeData.match(/\r\nEND\r\n/)) {
                                  console.log("Response: " + completeData);
                          }
                  }
          });
          conn.on('end', function() {
                  console.log("Connection closed to " + ip );
          });
          conn.on('error', function(err) {
                  console.log("Connection error: " + err + " for ip " + ip);
          });
  }
  </code></pre>
  
  <p>My biggest problem was apparently a logic error.  I was either waiting for the chunk that began the reply, or the chunk that ended it.  I wasn't saving everything in-between.</p>
  
  <p>I guess if I wanted to get all Node-ish about it, I should fire an event whenever a complete message came in (beginning with a blank line, ending with 'END' on a line by itself), and do the processing there.</p>