0
0
Node.jsframework~5 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js

Choose your learning style9 modes available
Introduction

Streams help handle data piece by piece instead of all at once. This saves memory and lets programs work faster with big data.

Reading a large file without loading it all into memory
Sending data over the internet bit by bit
Transforming data on the fly, like compressing or encrypting
Building a chat app where data flows both ways
Syntax
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.

Examples
A Readable stream that sends 'Hello' then ends.
Node.js
const readable = new Readable({
  read(size) {
    this.push('Hello');
    this.push(null); // No more data
  }
});
A Writable stream that prints whatever it receives.
Node.js
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});
A Transform stream that changes input text to uppercase.
Node.js
const transform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});
A Duplex stream that can read and write data.
Node.js
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();
  }
});
Sample Program

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.

Node.js
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);
OutputSuccess
Important Notes

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.

Summary

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.