6

I'm cutting my teeth with TCP sockets and am confused with how messages are arriving to my app. Seems like they are broken up. Anyone know how I can best join them back together? All messages are separated by a line break (\r\n)

var stream = net.createConnection(port,host); 
stream.setEncoding("utf8");

stream.on('data', function(chunk){
    console.log('data: ' + sys.inspect(chunk));
});

Sample of what dumps to the console looks like:

data: '0'
data: '5293800791D04\r'
data: '\n'
data: '053138007928F1\r\n'
data: '05313800012869\r\n'
data: '052E3800790E0E\r\n'
data: '052E3800010E86\r\n'
data: '05223'
data: '8007933F5\r\n'
data: '05213800791019\r\n'
data: '05213800795C795B79265A\r\n'
data: '05213800011091\r\n'

I need to break stuff up at the linebreak so I dont have incomplete messages. Is there a way to tell node to do this for me? If not, does anyone have any samples of how this might be done?

Anthony Webb
  • 1,311
  • 4
  • 15
  • 22

2 Answers2

7

I found a module called "carrier" which does exactly what I needed. After installing the module it was as easy as adding this:

carrier.carry(stream, function(line) {
    console.log("line: "+line)
})

I found the answers here:

http://nodetuts.com/tutorials/5-a-basic-tcp-chat-server.html

https://github.com/pgte/carrier

josh3736
  • 139,160
  • 33
  • 216
  • 263
Anthony Webb
  • 1,311
  • 4
  • 15
  • 22
5

Since TCP is a stream protocol (and not a block protocol), it does not maintain any boundaries between what you might consider to be individual messages. This is the responsibility of the receiver if you need to reassemble high-level messages from an incoming stream.

You can do this by adding a layer that reads input data into a buffer, and then releases one line at a time whenever it sees a \r\n in the input stream.

As an aside, if you can change the stream data to have just one separator character (such as \n only), the buffering code might become a bit simpler.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    You might want to say "messages" (an application-layer concept) instead of "data packets" (which has a specific meaning in TCP) to avoid confusion -- reassembling *data packets* into a stream is what TCP does. Turning that stream into *messages* is something an application does. – josh3736 Dec 25 '11 at 01:52