What is Piping?
You'll learn how piping automatically connects streams to transfer data seamlessly between them.
The Core Idea
Piping is a mechanism that connects a readable stream directly to a writable stream, automatically handling the flow of data between them. Instead of manually reading chunks from one stream and writing them to another, the pipe() method does all the heavy lifting for you—including backpressure management and error coordination.
Think of piping like connecting two garden hoses: once connected, water flows from one to the other automatically without you needing to manually transfer it bucket by bucket.
How It Works
When you call readableStream.pipe(writableStream), Node.js sets up internal listeners that:
- Read data from the readable stream as it becomes available
- Write that data to the writable stream
- Pause the readable stream if the writable stream gets overwhelmed (backpressure)
- Resume reading once the writable stream catches up
This all happens behind the scenes—you don't need to listen for 'data' events, check return values from write(), or handle 'drain' events yourself.
Why It Matters
Before learning about piping, you had to manually manage all stream interactions. Piping simplifies common operations like copying files, compressing data, or transforming content by letting you chain streams together declaratively. It's the foundation for building stream pipelines in Node.js.
Simple Mental Model
Readable Stream → pipe() → Writable Stream
(source) (destination)
The readable stream pushes data through the pipe, and the writable stream receives it—all automatically managed.
Key Takeaway: Piping connects readable and writable streams with pipe(), automating data transfer and backpressure management so you don't have to handle it manually.