Receiving data in node.js

Problem

I am having a java program send data to me over a specific socket to my node.js application. I want to be able to obtain all of the data, which is information from a SQlite database, and send it off to something else.

I've found something like the following can work but it seems to be unreliable as data is missing and sometimes it doesn't even show up.

stream.addListener('data', function(data){
buffer.write(data.toString());
});

on a side note, I need the socket to stay open so I can't call the "end" event.

I really don't have any attachment to stream.addListener so i can use something else if it works how i want. Basically what i'm asking is, What is the most effective way to obtain data from a socket using node.js?

P.S. thank you for your time

Problem courtesy of: John Marston

Solution

The data even is not guaranteed to have all the data sent to it in one go. You'll need to build up a buffer over multiple events and watch for delimiters of some kind (newlines, null characters, whatever you feel). Here's an example from a project where I'm parsing data from IRC (converted from CoffeeScript); parseData is the event handler for the data event (e.g. socket.on('data', this.parseData);):

IrcConnection.prototype.parseData = function(data) {
  var line, lines, i;

  data = data.replace("\r\n", "\n");
  this.buffer += data;
  lines = this.buffer.split("\n");
  this.buffer = "";

  /* Put the last line back in the buffer if it was incomplete */
  if (lines[lines.length - 1] !== '') {
    this.buffer = lines[lines.length - 1];
  }

  /* Remove the final \n or incomplete line from the array */
  lines = lines.splice(0, lines.length - 1);
  for (i = 0; i < lines.length; i++) {
    line = lines[i];
    this.emit('raw', line);
  }
};
Solution courtesy of: Michelle Tilley

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.