Streams are one of the most powerful and fundamental parts of Node.js. They let you process data chunk by chunk instead of loading it all at once. Let’s go through the three main types: Readable, Writable, and Transform.


πŸ“‚ 1. What Are Streams?

  • A stream is an abstract interface for working with streaming data.

  • Built on top of EventEmitter.

  • Useful for large files, networking, or real-time data.


πŸ“– 2. Readable Streams

A source of data that you can consume.
Examples: fs.createReadStream(), HTTP requests (req).

const fs = require("fs");
 
const readStream = fs.createReadStream("bigfile.txt", "utf8");
 
readStream.on("data", (chunk) => {
  console.log("Received chunk:", chunk.length);
});
 
readStream.on("end", () => {
  console.log("Finished reading file.");
});

Key events:

  • data β†’ emits chunks of data.

  • end β†’ no more data.

  • error β†’ something went wrong.


✍️ 3. Writable Streams

A destination you can write data into.
Examples: fs.createWriteStream(), HTTP responses (res).

const writeStream = fs.createWriteStream("output.txt");
 
writeStream.write("First line\n");
writeStream.write("Second line\n");
writeStream.end(); // finish the stream
 
writeStream.on("finish", () => {
  console.log("All data written!");
});

Key methods:

  • .write(chunk) β†’ write data.

  • .end() β†’ signal no more data will be written.


πŸ”„ 4. Piping (Readable β†’ Writable)

Easiest way to connect streams:

const readStream = fs.createReadStream("bigfile.txt");
const writeStream = fs.createWriteStream("copy.txt");
 
readStream.pipe(writeStream);

πŸ‘‰ Efficient: Node handles backpressure automatically (slowing down reading if writing is slower).


πŸ”€ 5. Transform Streams

A special type of Duplex stream that can read and write, but also modify data as it passes through.

Example: Convert text to uppercase:

const { Transform } = require("stream");
 
const upperCaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});
 
process.stdin.pipe(upperCaseTransform).pipe(process.stdout);

Usage:

  • Type something in terminal β†’ output is in uppercase.

βš™οΈ 6. Stream Types Recap

Stream TypeExample in Node.jsDirection
Readablefs.createReadStream(), http.IncomingMessageData out
Writablefs.createWriteStream(), http.ServerResponseData in
Duplexnet.Socket (read + write)Both
Transformzlib.createGzip(), custom transformsBoth + modify

βœ… In Short

  • Readable β†’ consume data (files, requests).

  • Writable β†’ send data (files, responses).

  • Transform β†’ modify data in the middle (compression, encryption).

  • Use .pipe() to connect streams efficiently and handle backpressure.


πŸ‘‰ Do you want me to go deeper into backpressure & stream.pipeline() (the modern safe way to connect streams), or move on to another Node.js core concept like child processes?