Chapter 29: Node.js Streams

1. What is a stream? (the simplest mental model)

A stream is a continuous flow of data that you can read from or write to piece by piece — instead of loading everything into memory at once.

Think of it like a water pipe:

  • You don’t wait for the whole river to arrive before you start using water
  • You use it as it flows
  • You can connect pipes → send water from one pipe to another

Four main types of streams in Node.js

Type What it does Readable? Writable? Classic real-world example
Readable You read data from it Yes No Reading a large file, HTTP response, stdin
Writable You write data to it No Yes Writing to a file, HTTP request body, stdout
Duplex Can read and write (both directions) Yes Yes TCP socket, WebSocket connection
Transform A special Duplex — reads, modifies, writes Yes Yes gzip compression, JSON parsing, uppercase text

The golden rule everyone must remember:

Streams exist so you never load huge data entirely into memory They process data in small chunks (usually ~64 KB by default)

This is why Node.js can handle gigabyte-sized files or video streaming with very low memory usage.

2. The classic “don’t do this” example (very important)

Wrong way — loads entire file into memory

JavaScript

→ If file is 10 GB → your server dies (out of memory)

Right way — using streams (memory usage stays almost constant)

JavaScript

→ Memory usage ≈ 64 KB no matter how big the file is

3. All four types with real, runnable examples

3.1 Readable Stream – reading data chunk by chunk

JavaScript

Real-world use case: Reading CSV / JSON lines / log files too large for memory

3.2 Writable Stream – sending data chunk by chunk

JavaScript

Important: .write() returns false when the internal buffer is full → you should wait for ‘drain’

JavaScript

3.3 Piping – the most beautiful feature of streams

JavaScript

Multiple pipes (very common)

JavaScript

3.4 Duplex & Transform – the most powerful ones

Transform example – real use-case

JavaScript

Run → type text → every “hello” becomes “HI”

Very common real-world transform examples:

  • Gzip / gunzip
  • JSON parsing line-by-line (JSONStream, ndjson)
  • CSV parsing (csv-parser)
  • Encryption / decryption
  • Image resizing on-the-fly
  • Replacing text / sanitizing HTML

5. Modern best practices & patterns (2025–2026)

Pattern 1 – Pipe + error handling (very important)

JavaScript

Pattern 2 – Promise wrapper for streams (very clean)

JavaScript

Pattern 3 – HTTP file download using streams

JavaScript

→ Browser can start playing video before entire file is read

6. Common mistakes & how to avoid them

Mistake Consequence Fix
Not handling ‘error’ on streams Silent failures, memory leaks Always add .on(‘error’, …)
Not calling .end() on writable stream Client hangs forever Always call .end() when done writing
Using .pipe() without error handling Errors disappear Use pipeline() from stream/promises
Loading entire stream into memory Out of memory on large files Use .pipe() or process chunks
Forgetting to resume paused stream Data loss / hanging Understand backpressure & ‘drain’

Modern helper (highly recommended)

JavaScript

→ Automatically handles errors & cleanup

Summary – Quick decision guide

You want to… Use this type / pattern Typical real-world example
Read large file / response Readable stream + .pipe() File download, log tailing, HTTP streaming
Write large amount of data Writable stream + .write() + .end() Generating CSV, logging, uploading to S3
Transform data on-the-fly Transform stream Compression, encryption, text replacement
Connect streams safely stream/promises.pipeline() Modern, promise-based, error-safe piping
Handle backpressure Listen for ‘drain’ event Writing very fast to slow destination

Would you like to go much deeper into any part?

  • Backpressure – how to handle it properly
  • pipeline() vs manual .pipe() – when & why
  • Object mode streams (very powerful)
  • Real production example – file upload + resize + S3 stream
  • Stream error handling patterns in Express/Fastify

Just tell me which direction feels most useful — I’ll continue with detailed, production-ready code examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *