Full answer
A Readable has an internal buffer. How you consume it decides the mode.
- Initial state: readableFlowing is null. Nothing is read from the source.
- Flowing: attach a 'data' listener, call pipe(), or call resume(). The stream reads from the source and emits 'data' for every chunk. If you have no consumer ready, chunks can be lost, so attach listeners right away.
- Paused: call pause(), or attach a 'readable' listener. Data stays in the buffer. 'readable' tells you data is available, and you call read() in a loop until it returns null.
- The 'readable' listener takes priority. If it is attached, 'data' does not flow until the listener is removed.
Which to use:
- for await (const chunk of readable) is the modern default. It pulls data, handles errors, and ends cleanly.
- pipeline for connecting streams.
- 'data' for quick scripts and for events that you want to push to listeners as they happen.
- 'readable' with read(n) when you need to read an exact number of bytes, for example a binary protocol header.
Important rules: do not mix styles on the same stream, always handle 'error', and never rely on 'data' being called before you attach the listener. The legacy 'streams1' pause behaviour is gone, so do not rewrite old code unless needed.
javascript
const { Readable } = require('node:stream');
const make = () => Readable.from(['a', 'b', 'c'], { objectMode: false });
// 1. States
const r1 = make();
console.log('initial readableFlowing:', r1.readableFlowing);
r1.on('data', () => {});
console.log('after data listener :', r1.readableFlowing);
r1.pause();
console.log('after pause() :', r1.readableFlowing);
r1.destroy();
// 2. 'data' event
const r2 = make();
const got = [];
r2.on('data', (chunk) => got.push(chunk.toString()));
r2.on('end', () => {
console.log("'data' event pieces :", got.join(','));
// 3. 'readable' event with read()
const r3 = Readable.from(['x', 'y', 'z'], { objectMode: false });
const pulled = [];
r3.on('readable', () => {
let chunk;
while ((chunk = r3.read()) !== null) pulled.push(chunk.toString());
});
r3.on('end', async () => {
console.log("'readable' pieces :", pulled.join(','));
// 4. async iteration
let text = '';
for await (const chunk of make()) text += chunk.toString();
console.log('for await text :', text);
});
});The first lines print readableFlowing in each state. The next lines show the same data read through three styles. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): initial readableFlowing: null after data listener : true after pause() : false 'data' event pieces : a,b,c 'readable' pieces : xy,z for await text : abc
Key points
- Readable streams start paused (readableFlowing is null).
- 'data', pipe and resume() switch to flowing mode.
- 'readable' keeps the stream paused and you call read().
- Prefer for await...of or pipeline.
Common mistakes
- Attaching the 'data' listener late and missing chunks.
- Mixing 'data' and 'readable' on one stream.
- Not handling 'error'.
- Calling read() once per 'readable' event instead of looping until null.
- Assuming each chunk is one line or one message.
Follow-ups
- What does pause() do to a pipe()d stream?
- How does read(size) behave when fewer bytes are buffered?
- Why is for await safer than the 'data' event?
Tips
- Start with the three states of readableFlowing.
- Say you prefer for await.
- Mention read(n) for binary headers.