What Are Streams in Node.js: Explanation and Examples
Streams in Node.js are objects that let you read or write data piece by piece instead of all at once. They help handle large data efficiently by processing it in chunks, like reading a book page by page rather than the whole book at once.How It Works
Imagine you want to drink a large bottle of water. Instead of pouring it all at once and risking spilling, you take small sips. Streams in Node.js work similarly by handling data in small parts called chunks. This way, your program can start working on the data immediately without waiting for everything to load.
Streams come in four types: readable (to get data), writable (to send data), duplex (both read and write), and transform (modify data while passing it through). They use events to notify your program when new data is ready or when the process finishes, making data handling efficient and memory-friendly.
Example
This example shows how to read a text file using a readable stream and print its content chunk by chunk.
import { createReadStream } from 'node:fs'; const stream = createReadStream('example.txt', { encoding: 'utf8' }); stream.on('data', (chunk) => { console.log('Received chunk:', chunk); }); stream.on('end', () => { console.log('No more data.'); }); stream.on('error', (err) => { console.error('Error:', err.message); });
When to Use
Use streams when working with large files or data that you don't want to load entirely into memory, such as video files, logs, or network data. They are perfect for real-time data processing, like reading user uploads or sending data over the internet in pieces.
Streams improve performance and reduce memory use, making your Node.js applications faster and more scalable.
Key Points
- Streams process data in small chunks, not all at once.
- They help handle large data efficiently without using much memory.
- Four types: readable, writable, duplex, and transform.
- Use events like
data,end, anderrorto manage streams. - Ideal for files, network communication, and real-time data.