In Node.js, what is the difference between flowing and paused mode on a stream.Readable, and which calls switch a stream between them?
answer
- push versus pull
- readableFlowing has three values
- data listener, resume(), pipe()
- removing listeners does not pause
basics
~20 sIn flowing mode a Readable pushes chunks to 'data' listeners as soon as they arrive; in paused mode you pull them with read(). A 'data' listener, resume() or pipe() starts the flow; pause() or unpiping every destination stops it.
solid answer
~40 sEvery `Readable` starts with no consumer (`readableFlowing === null`) and generates no data. Attaching a `'data'` listener, calling `resume()` or calling `pipe()` switches it to **flowing**: chunks are emitted as `'data'` events as soon as they are available. Calling `pause()` when there are no pipe destinations, or `unpipe()`-ing all of them, sets `readableFlowing` to `false`: chunks stay in the internal buffer and you pull them with `read()`, usually inside a `'readable'` handler. Attaching a `'readable'` listener also stops the flow and takes precedence over `'data'`, and while it is attached `pause()` and `resume()` do nothing. Two traps: removing `'data'` listeners does not pause the stream, and a stream switched to flowing with nobody consuming loses those chunks.
code
javascript · 11 linesimport { PassThrough } from 'node:stream';
const pass = new PassThrough();
console.log(pass.readableFlowing); // null
pass.pause();
console.log(pass.readableFlowing); // false
pass.on('data', (chunk) => console.log('data:', chunk.toString()));
console.log(pass.readableFlowing); // false: an explicit pause wins over the new listener
pass.write('ok'); // buffered; no 'data' yet
pass.resume();
console.log(pass.readableFlowing); // true; 'data: ok' prints on a later tickgo deeper
Remember that a 'data' listener makes a Readable push chunks to you, and that pause() and resume() stop and restart that push.
Walk through the three readableFlowing states, each call that moves between them, and how 'readable' with read() pulls from the buffer.
Use the modes to diagnose: why chunks vanished after a listener was removed, why resume() had no effect, and why mixing consumption styles misbehaved.
Set the team rule of one consumption style per stream, preferring for await or pipeline over hand-rolled 'data' plumbing in service code.
## Two modes, three states A `stream.Readable` hands data to your code in one of two **reading modes**: - **Flowing mode** is *push*: the stream reads from its source on its own and emits each chunk as a `'data'` event as soon as it is available. - **Paused mode** is *pull*: chunks wait in the stream's **internal buffer** until your code calls `readable.read()`. The two modes are a simplification of three internal states, visible through the `readable.readableFlowing` property: | `readableFlowing` | Meaning | Does the stream produce data? | |---|---|---| | `null` | no consumer has been attached yet | no; it waits for a consumer | | `false` | explicitly paused (or unpiped, or held by backpressure) | yes, into the buffer, but emits no `'data'` | | `true` | flowing | yes, emitted as `'data'` events | The docs put the rule plainly: a Readable will not generate data until a mechanism for consuming or ignoring it is provided. That is why a freshly created file stream with nothing attached does not lose anything. ## What starts the flow From `null`, any of these switches the stream to flowing (`true`): 1. Attaching a `'data'` listener. 2. Calling `readable.resume()`. 3. Calling `readable.pipe(destination)`. There is a subtlety: once a stream has been **explicitly paused** (`readableFlowing === false`), attaching a `'data'` listener no longer restarts it. Only `resume()` does. ## What stops it - `readable.pause()` stops `'data'` events when there are **no pipe destinations**; with pipe destinations it is not guaranteed to stay paused once they drain and ask for more. - Removing every pipe destination with `readable.unpipe()` also leaves the stream paused. - Attaching a **`'readable'` listener** stops the flow: the stream now waits for `read()` calls. While a `'readable'` listener is attached, `pause()` and `resume()` have **no effect**, and `'data'` fires only when `read()` returns a chunk. - **Removing `'data'` listeners does not pause the stream.** This is kept for backward compatibility, and it is the most common surprise. ## Pulling with 'readable' and read() In paused mode the usual loop is: ```js readable.on('readable', () => { let chunk; while ((chunk = readable.read()) !== null) { handle(chunk); } }); readable.on('end', () => console.log('done')); ``` - `'readable'` means "there is something in the buffer" (or the end has been reached). - `read()` returns a chunk, or `null` when the buffer is empty for now; a later `'readable'` fires when more arrives. - `'end'` fires only after the data has been fully consumed. - The docs note that `pipe()` and `'data'` are easier to reason about, while `'readable'` *might* give higher throughput. ## Where data goes missing Flowing mode is push, so a chunk emitted with no one listening is gone. The docs name two ways this happens: - calling `resume()` with no `'data'` listener attached (a deliberate way to discard a stream's contents, and an accidental way to lose them); - removing a `'data'` handler while the stream keeps flowing. The same thing happens when one `'data'` listener (a byte counter, say) starts the flow and the real consumer is attached later: the chunks emitted in between went only to the first listener. ## Inspecting the state while debugging When a stream seems stuck or data seems to vanish, these tell you which mode it is really in: - `readable.readableFlowing` shows the three-state value directly. - `readable.readableLength` is the number of bytes (or objects, in object mode) waiting in the buffer; a non-zero value that stays put while nothing is emitted means the stream is paused and no one is calling `read()`. - The `'pause'` event fires when `pause()` is called on a stream that was not already paused, and `'resume'` fires when `resume()` is called on one that was not already flowing; logging both shows who toggled the mode. - `readable.isPaused()` exists mainly for the machinery behind `pipe()`; the docs say ordinary code rarely needs it. - `readable.readableEnded` tells you whether `'end'` has already been emitted, which explains a listener attached too late. ## Choose one consumption style `'data'`, `'readable'`, `pipe()` and `for await...of` are four ways to consume a Readable, and the docs say to pick **one** per stream: combining them leads to unintuitive behaviour, for example a `'readable'` listener silently taking control away from a `'data'` listener. Async iteration is built on the paused-mode `read()` path, which is why it composes cleanly with nothing else attached.
- If you remove every 'data' listener from a flowing Readable, does it stop reading?No. For backward compatibility, removing `'data'` handlers does not pause the stream. It keeps flowing, and chunks emitted with no listener are lost. To stop the flow, call `pause()` (when nothing is piped) or attach a `'readable'` listener and switch to `read()`.
- What does readable.read() return when the buffer is empty but the stream has not ended?`null`. That means nothing is buffered right now, not that the stream has ended. You stop the `read()` loop and wait for the next `'readable'` event; `'end'` is emitted only once the source is finished and the buffer has been fully consumed.
- Why is 'readable' plus read() sometimes preferred over 'data'?It lets the consumer decide when and how much to pull: you can drain everything currently buffered in one loop, read a specific number of bytes with `read(size)`, or simply stop reading. The docs say it might give higher throughput, while `'data'` and `pipe()` are easier to reason about.
- A Readable starts in flowing mode and emits data immediately.
- Removing the last 'data' listener pauses the stream.
- pause() discards incoming chunks until resume() is called.
- Calling read() is the normal way to consume a flowing stream.
- Mixing a 'data' listener and a 'readable' listener on one stream is harmless.