9

The documentation for write() says:

Returns false to indicate that the kernel buffer is full, and the data will be sent out in the future.

and the documentation for the drain event says:

After a write() method returned false, this event is emitted to indicate that it is safe to write again.

What does that mean? Do I have to wait for the drain event before I can write again? What happened to the data which I tried to write? Is that lost? What happens when I call write without waiting for the drain event?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

2 Answers2

7

@TooTallNate answer is wrong. You should absolutely care about .write return value.

What does that mean? Do I have to wait for the drain event before I can write again?

Yes, when .write returns false, you must wait.

What happened to the data which I tried to write? Is that lost?

No, it is not lost, it's buffered in the correct order.

What happens when I call write without waiting for the drain event?

If you don't handle the drain event correctly, your script will block the event loop, and eventually exhaust the memory limit crashing your application.

I give a detail explanation in this other question: why does attempting to write large a large file cause js heap to run out of memory

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
4

You can call write() without caring about the return value safely. Node will buffer any write calls while the kernel buffer is full, and push them in order as you would expect. You do not need to wait for the 'drain' event before writing again.

Optionally, you can check the return value of write() and then notify the thing that is writing to the Stream that the buffer is full. This is exactly what Stream#pipe() does.

So usually, just use Stream#pipe() and all the goodies are taken care of for you :)

TooTallNate
  • 1,477
  • 3
  • 20
  • 41
  • 1
    What happens if you call `process.exit()` while the `res.write` is still buffered in user memory (i.e. not flushed to the kernel buffer)? Does the response get lost? – Emmett Apr 23 '14 at 21:32
  • @Emmett yes, if you call `process.exit()` while there's buffered write calls remaining to be flushed then that data will be lost. – TooTallNate May 25 '14 at 15:55
  • 8
    It's not entirely safe to "call `write()` without caring about the return value". The [current docs](https://nodejs.org/dist/v8.4.0/docs/api/stream.html) say: _"While calling `write()` on a stream that is not draining is allowed, Node.js will buffer all written chunks until maximum memory usage occurs, at which point it will abort unconditionally. Even before it aborts, high memory usage will cause poor garbage collector performance and high RSS (which is not typically released back to the system, even after the memory is no longer required)."_ Also see: https://stackoverflow.com/q/37579605 – TachyonVortex Aug 27 '17 at 14:34