0
0
Node.jsframework~10 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stream types (Readable, Writable, Transform, Duplex)
Start Stream Operation
Readable
Data Out
Stream Ends or Pipes to Next
This flow shows how a stream operation starts by choosing one of four stream types, each handling data flow differently.
Execution Sample
Node.js
import { Readable, Writable, Transform, Duplex } from 'stream';

const readable = new Readable({
  read() { this.push('Hello'); this.push(null); }
});

const writable = new Writable({
  write(chunk, _, cb) { console.log(chunk.toString()); cb(); }
});

readable.pipe(writable);
This code creates a readable stream that outputs 'Hello' and a writable stream that logs data, then pipes readable to writable.
Execution Table
StepActionStream TypeData ChunkOutput/Effect
1Create Readable streamReadableStream ready to emit data
2Create Writable streamWritableStream ready to receive data
3readable.pipe(writable)Readable -> WritableConnects streams, triggers readable.read()
4Readable read() calledReadable'Hello'Pushes 'Hello' to internal buffer, pipe sends to writable
5Readable pushes nullReadablenullSignals end of data
6Writable write() calledWritable'Hello'Logs 'Hello' to console
7Writable write() callbackWritableSignals write complete
8Stream endsReadable & WritableNo more data, streams close
💡 Streams end after readable pushes null and writable finishes writing
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
readable internal bufferemptyempty'Hello'null (end)empty (piped out)closed
writable received dataemptyempty'Hello''Hello''Hello'closed
Key Moments - 3 Insights
Why does the readable stream push null after 'Hello'?
Pushing null signals the end of data, so writable knows no more data is coming (see Step 5 in execution_table).
What happens when readable.pipe(writable) is called?
It connects readable output to writable input, so data flows automatically from readable to writable (see Step 3).
Why does writable call a callback after writing?
The callback signals that the write is complete and the stream can accept more data or end (see Step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what data chunk does the readable stream push first?
A'Hello'
Bnull
C'World'
Dempty string
💡 Hint
Check Step 4 in the execution_table where readable pushes data
At which step does the writable stream receive data from readable?
AStep 3
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the Readable read() call where data is piped to writable in execution_table
If readable did not push null, what would happen?
AReadable would throw an error
BWritable would never know the stream ended
CWritable would close immediately
DData would be lost
💡 Hint
Refer to Step 5 where pushing null signals end of data
Concept Snapshot
Node.js streams handle data flow in four types:
- Readable: outputs data
- Writable: accepts data
- Transform: modifies data as it passes
- Duplex: both readable and writable
Use pipe() to connect streams for smooth data transfer.
Push null in readable to signal end.
Full Transcript
Node.js streams come in four types: Readable streams produce data, Writable streams consume data, Transform streams modify data while passing it through, and Duplex streams can do both reading and writing. In the example, a Readable stream pushes the string 'Hello' and then pushes null to signal no more data. A Writable stream receives this data and logs it. The pipe() method connects the readable to the writable, so data flows automatically. The writable calls a callback after writing to signal completion. Pushing null is important to tell the writable stream that the data has ended, so it can close properly.