jmesnil/stomp-websocket — pull request #68 by Puigcerber (2014-05-30 09:02:58+00:00)
Title: Consume the data from the stream.

I noticed I was getting truncated messages.

My guess it's that Node.js it's **getting messages faster than it can process** them and as the TCP protocol it's stream oriented, Node.js has no idea of what a message/frame it's.

We need to handle the [net event data](http://nodejs.org/api/net.html#net_event_data) which is a [readable stream](http://nodejs.org/api/stream.html). The argument itself can be a `Buffer` or a `String`; for it to be a string we would need to set up the encoding as `socket.setEncoding('utf8')`.

So the data that we get on the `'data'` event it's actually a chunk of data. I have created a buffer object where we accumulate these chunks of data. To get the frames we split the buffered data by the null byte which can be followed by multiple newlines as described in the [STOMP protocol](http://stomp.github.io/stomp-specification-1.1.html#STOMP_Frames) definition. On this way we just send completed frames to the `onmessage` method.