Advanced Streams
๐ Covered in this chapter:
pipeline() ยท Backpressure & highWaterMark ยท File Upload Streaming ยท HTTP Response Streaming
Go deeper into Node's stream ecosystem: the pipeline() helper, backpressure tuning, and streaming file uploads and HTTP responses.
1Advanced Streams โ What You'll Learn
Go deeper into Node's stream ecosystem: the pipeline() helper, backpressure tuning, and streaming file uploads and HTTP responses.
Here's everything this chapter covers, in the order you'll learn it:
- Revisiting Readable, Writable, Duplex and Transform streams
- pipeline() for safe, error-handled stream chaining
- Backpressure in depth
- The highWaterMark option
- Handling stream errors robustly
- Streaming file uploads
- Compression streams (gzip) on the fly
- Streaming HTTP responses
- Measuring and improving stream performance
2Working Example
๐ป Example: Advanced Streams
JavaScript
โถ Run in Compiler
import { pipeline } from "node:stream/promises";
import { createReadStream, createWriteStream } from "node:fs";
import { createGzip } from "node:zlib";
await pipeline(
createReadStream("input.txt"),
createGzip(),
createWriteStream("input.txt.gz")
);
console.log("File compressed successfully");
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- pipeline() (over manual .pipe() chaining) automatically forwards errors and cleans up all streams if any one of them fails โ always prefer it in production code.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about advanced streams?
Focus on: pipeline() ยท Backpressure & highWaterMark ยท File Upload Streaming ยท HTTP Response Streaming. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for advanced streams?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.