Streams help handle data piece by piece instead of all at once. This saves memory and lets programs work faster with big data.
Stream types (Readable, Writable, Transform, Duplex) in Node.js
const { Readable, Writable, Transform, Duplex } = require('stream');These are classes you can use to create different stream types.
Each stream type has specific methods to implement for your data flow.
const readable = new Readable({
read(size) {
this.push('Hello');
this.push(null); // No more data
}
});const writable = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
});const transform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});const duplex = new Duplex({
read(size) {
this.push('Data from read side');
this.push(null);
},
write(chunk, encoding, callback) {
console.log('Received:', chunk.toString());
callback();
}
});This program creates three streams. The readable stream sends numbers 1, 2, and 3. The transform stream doubles each number. The writable stream prints the doubled numbers. The streams are connected so data flows from readable to transform to writable.
const { Readable, Writable, Transform } = require('stream');
// Readable stream sends numbers 1 to 3
const readable = new Readable({
read() {
if (!this.current) this.current = 1;
if (this.current <= 3) {
this.push(this.current.toString());
this.current++;
} else {
this.push(null); // End
}
}
});
// Transform stream doubles the number
const transform = new Transform({
transform(chunk, encoding, callback) {
const num = Number(chunk.toString());
this.push((num * 2).toString());
callback();
}
});
// Writable stream prints the result
const writable = new Writable({
write(chunk, encoding, callback) {
console.log('Output:', chunk.toString());
callback();
}
});
// Connect streams: readable -> transform -> writable
readable.pipe(transform).pipe(writable);Always call callback() in writable and transform streams to signal you finished processing.
Use this.push(null) in readable streams to mark the end of data.
Duplex streams combine readable and writable in one object for two-way data flow.
Readable streams provide data piece by piece.
Writable streams accept and process data.
Transform streams change data while passing it along.
Duplex streams can both read and write data.