Node.js – Streams Module
Subject: nodejs
Node.js – Streams Module
The Streams Module in Node.js is used for handling streamed data, such as reading or writing files, network communications, or any continuous flow of data. Streams provide an efficient way to process large amounts of data without loading everything into memory.
What is a Stream?
A stream is an abstract interface for working with streaming data in Node.js. It allows reading or writing data piece-by-piece (in chunks), rather than all at once.
Types of Streams
Node.js provides four main types of streams:
- Readable – Read data from a source (e.g., file, network)
- Writable – Write data to a destination
- Duplex – Both read and write
- Transform – Modify or transform data during read/write
Why Use Streams?
- Memory-efficient: Handle large data without loading all of it into memory.
- Faster processing: Start processing as data arrives.
- Non-blocking: Great for real-time applications.
1. Readable Stream Example
Reading a file using a stream:
2. Writable Stream Example
Writing data to a file:
3. Piping Streams (Read + Write)
Using .pipe()
to connect readable and writable streams:
.pipe()
simplifies the process of transferring data from one stream to another.
4. Transform Stream Example
Using the built-in zlib
module to compress a file:
Events in Streams
Streams emit several events to track data flow:
- data: Emitted when a chunk of data is available
- end: No more data to read
- error: Error occurred during stream operation
- finish: All data written (for writable streams)
Key Takeaways
- Streams are used to read and write data efficiently.
- There are four types: Readable, Writable, Duplex, and Transform.
- Use
.pipe()
to connect readable and writable streams. - Streams are essential for file processing, real-time apps, network communication, and performance-sensitive operations.