Chris Evans
Chris Evans

Reputation: 993

Buffers, sockets. node.js

I'm trying to utilise TCP sockets in Node.js to communicate with a Lua program. There are two issues I'm trying to get my head around. Firstly buffers.

Buffers As I understand it, when data is provided to your code from the socket, it will be whatever data has been received so far (a stream rather than packets). So when you read the data received, it may be broken from what was sent.

i.e. data received { schools : ["Long

vs data sent { schools : [{"Longwood", "Hillbrow"}] }

The way to get around this is to put your data into a 'buffer' and split it by whatever method you use to show the end of that piece of information. Commonly appears to be new line.

My questions here:

  1. Can you end up with more 2 or more complete sections of data in the buffer, how would you handle this? For loop?
  2. The same issue appears to be present with data leaving the socket. I have noticed however in other code examples, people use the Node.js Buffer before writing to the socket. Why is this not used for incoming data?
  3. If more data is written than can be handled to leave, does Node handle this for you or do you need to come up with a method.

Finally I seem to be misunderstanding the data side. Does all data sent and received need to be converted to binary and back? I wish to send JSON data back and forth only. I think there in lies my confusion. For example:

var myQuestion = "Is this acceptable and will I encounter any issues?
socket.write(myQuestion);

Many thanks for your time.

Upvotes: 4

Views: 6568

Answers (2)

loganfsmyth
loganfsmyth

Reputation: 161447

  1. Yes, you could definitely end up with several sections of data. It entirely depends on the code you write on either end.
  2. I don't quite follow what you mean by "use the buffer before writing". Buffers are just pure binary data, they are used for both incoming and outgoing data. Node also generally accepts strings for sending, and assumes they are UTF8 if no encoding is given. You can also get strings when receiving if you call setEncoding.
  3. The OS has buffers for incoming data on the TCP socket, and when the buffer is full (being emptied too slowly by node), it will stop accepting packets until there is space.

First off, Buffer is binary. A buffer is simply an array of bytes, and nothing more. Buffers can be created from strings, and node will do this automatically in some cases. In your case, I would recommend that you call socket.setEncoding('utf8'). That will automatically convert incoming data into a string to simplify your parsing.

As far as processing and splitting the data, it is up to you. TCP only provides a stream of bytes arriving in the same order they were send. As you said, you can gather up the bytes and when a newline is received, you take everything before that and parse it as JSON. This should work fine. You can use any character that wouldn't pop up inside JSON. As long as the programs doing the JSON serialization do not add newlines, then you are set. As 'data' is emitted, you can check the string for newline, and if not, then add it to any previously received data, and if you find it, then split it up, add the existing data.

Upvotes: 5

Brad
Brad

Reputation: 163272

Buffers are used for information sent and received. Data received can be fragmented, as you have noticed. It is always up to your code to make this data back into its correct pieces.

It is entirely possible to have two chunks of data received in a buffer. Usually, you will have a delimiter of some kind (such as a new line, like you mentioned). What I do is this:

  1. When data comes in, concatenate it onto the end of a received data buffer.
  2. Have a function that reads from this buffer, splitting on your logical packet delimiter
  3. Validate that logical packet.
  4. If that packet is valid and complete, then raise an event to the higher level part of your code that a full piece of information has been received. Raise this event with your JSON all parsed into a regular object.

Upvotes: 1

Related Questions