Streams let you process data piece by piece instead of loading everything into memory. Essential for handling large files and real-time data.
Why Streams?
Stream Types
- Readable: Source of data (fs.createReadStream, http.IncomingMessage)
- Writable: Destination (fs.createWriteStream, http.ServerResponse)
- Transform: Modify data passing through (zlib.createGzip)
- Duplex: Both readable and writable (net.Socket)
The pipe() Method
Transform Streams
Modern Async Iteration
Backpressure
When the writable stream can't keep up with the readable stream, backpressure builds up. pipe() handles this automatically. If using manual events, check the return value of write() and wait for the drain event.