Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Teo Inke
Teo Inke
4,403 Points

Since Node is non-blocking, how data chunks arrive in order?

I have a guess but wanted someone to confirm/correct me or maybe explain it further. Since these chunks come in data packages over the network and it's not in order, does Node buffer and release them sequentially to the application? Ex: data packages 1, 3, 4, 5, 6, 7, 8 have arrived. It will then store 3-8 on a buffer until 2 arrive, then it "releases" all of them in order to the application. Is it how it works? I know TCP does it but not sure if Node has to do it in the application level.

1 Answer

rydavim
rydavim
18,814 Points

I believe you are correct. When writing to a stream, data is either queued or written, so the order does not change. This is taken care of by Node.js Stream. The data event doesn't happen for a chunk unless there is no queued data, so you'll get it in the order it was passed.

If you're interested in the technical stuff, this is the writeOrBuffer function from Node.js:

function writeOrBuffer(stream, state, chunk, encoding, cb) {
  chunk = decodeChunk(state, chunk, encoding);
  if (util.isBuffer(chunk))
    encoding = 'buffer';
  var len = state.objectMode ? 1 : chunk.length;

  state.length += len;

  var ret = state.length < state.highWaterMark;
  state.needDrain = !ret;

  if (state.writing || state.corked)
    state.buffer.push(new WriteReq(chunk, encoding, cb));
  else
    doWrite(stream, state, false, len, chunk, encoding, cb);

  return ret;
}

I found this article on streams helpful. This stream handbook on github is also good, and more in-depth. Of course there's always the Node.js documentation.

Hopefully that makes sense. Happy coding! :)