Streams in Node.js
๐ Covered in this chapter:
Readable/Writable Streams ยท pipe() ยท Backpressure ยท File & HTTP Streaming
Process large amounts of data efficiently โ piece by piece โ using Node's Readable, Writable, Duplex and Transform streams.
1Streams in Node.js โ What You'll Learn
Process large amounts of data efficiently โ piece by piece โ using Node's Readable, Writable, Duplex and Transform streams.
Here's everything this chapter covers, in the order you'll learn it:
- What a stream is and why it matters for large data
- Readable streams
- Writable streams
- Duplex streams (both readable and writable)
- Transform streams (modify data as it flows through)
- pipe() to connect streams together
- Backpressure and why it matters
- Handling stream errors
- Streaming files instead of loading them fully into memory
- HTTP request/response streaming
- Stream-based promises
- Compression streams
2Working Example
๐ป Example: Streams in Node.js
JavaScript
โถ Run in Compiler
import { createReadStream } from "node:fs";
const stream = createReadStream("large-file.txt", "utf8");
stream.on("data", (chunk) => {
console.log("Received chunk:", chunk.length);
});
stream.on("end", () => {
console.log("Finished");
});
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- Streams let you process files far larger than your available RAM, because data flows through in small chunks instead of loading everything at once.
- Always use .pipe() (or the pipeline() helper from node:stream) rather than manually handling 'data' events, since it automatically manages backpressure and errors.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about streams in node.js?
Focus on: Readable/Writable Streams ยท pipe() ยท Backpressure ยท File & HTTP 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 streams in node.js?
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.