title: NodeJS: What is the proper way to handling TCP socket streams ? Which delimiter should I use?
body: <p>From what I understood <a href="http://nodejs.org/jsconf2010.pdf" rel="noreferrer">here</a>, "V8 has a generational garbage collector. Moves objects aound randomly. Node can’t get a pointer to raw string data to write to socket." so I shouldn't store data that comes from a TCP stream in a string, specially if that string becomes bigger than <code>Math.pow(2,16)</code> bytes. (hope I'm right till now..)</p>
  
  <p>What is then the best way to handle all the data that's comming from a TCP socket ? So far I've been trying to use <code>_:_:_</code> as a delimiter because I think it's somehow unique and won't mess around other things.</p>
  
  <p>A sample of the data that would come would be <code>something_:_:_maybe a large text_:_:_ maybe tons of lines_:_:_more and more data</code></p>
  
  <p>This is what I tried to do:</p>
  
  <pre><code>net = require('net');
  var server = net.createServer(function (socket) {
      socket.on('connect',function() {
          console.log('someone connected');
          buf = new Buffer(Math.pow(2,16));  //new buffer with size 2^16
          socket.on('data',function(data) {
              if (data.toString().search('_:_:_') === -1) {    // If there's no separator in the data that just arrived...
                  buf.write(data.toString());   // ... write it on the buffer. it's part of another message that will come.
              } else {        // if there is a separator in the data that arrived
                  parts = data.toString().split('_:_:_'); // the first part is the end of a previous message, the last part is the start of a message to be completed in the future. Parts between separators are independent messages
                  if (parts.length == 2) {
                      msg = buf.toString('utf-8',0,4) + parts[0];
                      console.log('MSG: '+ msg);
                      buf = (new Buffer(Math.pow(2,16))).write(parts[1]);
                  } else {
                      msg = buf.toString() + parts[0];
                      for (var i = 1; i &lt;= parts.length -1; i++) {
                          if (i !== parts.length-1) {
                              msg = parts[i];
                              console.log('MSG: '+msg);
                          } else {
                              buf.write(parts[i]);
                          }
                      }
                  }
              }
          });
      });
  });
  
  server.listen(9999);
  </code></pre>
  
  <p>Whenever I try to <code>console.log('MSG' + msg)</code>, it will print out the whole buffer, so it's useless to see if something worked.</p>
  
  <p>How can I handle this data the proper way ? Would the lazy module work, even if this data is not line oriented ? Is there some other module to handle streams that are not line oriented ?</p>