Node.js TCP input buffering

buffer, node.js, socket.io, sockets, tcp

Solution

I think this problem has been tackled in this article: http://openmymind.net/2012/1/12/Reading-From-TCP-Streams/

Links to the 2 solutions described there:

- https://github.com/karlseguin/redispy/blob/master/lib/reader.coffee

- https://gist.github.com/1598236

Problem

I'm not sure I got this right yet, but as I understand it, when I attach a handler to the `data` event, it gives me all the data it got until then. The problem is, how do I handle buffering that data, as Node.js might give me less that 4 bytes at a time (which is the minimum I need), especially when there will be multiple concurrent requests?

Original source