Streams in Node.js are a powerful way to handle reading and writing data efficiently, especially when dealing with large amounts of data. Instead of loading an entire file or buffer into memory at once, streams allow you to process data in chunks as it's being read or written, making them ideal for large files or data sources.
Types of Streams in Node.js
-
Readable Streams: These allow you to read data, one chunk at a time.
-
Examples:
fs.createReadStream()
,http.IncomingMessage
,process.stdin
-
-
Writable Streams: These allow you to write data, one chunk at a time.
-
Examples:
fs.createWriteStream()
,http.ServerResponse
,process.stdout
-
-
Duplex Streams: These are both readable and writable.
-
Examples:
net.Socket
,tls.TLSSocket
-
-
Transform Streams: These are a type of duplex stream that modifies data as it's being read and written.
-
Examples:
zlib.createGzip()
(for compression),crypto.createCipher()
(for encryption)
-
How Streams Work
1. Readable Stream
A readable stream reads data from a source (e.g., a file or HTTP request) and makes it available in small chunks.
-
'data'
event: Fired when a chunk of data is available to read. -
'end'
event: Fired when the stream has no more data to read.
2. Writable Stream
A writable stream writes data to a destination (e.g., a file or HTTP response) in small chunks.
-
write()
method: Used to send data to the writable stream. -
end()
method: Signals that no more data will be written.
3. Piping Streams
You can connect a readable stream to a writable stream using the pipe() method. This allows data to flow from one stream to another automatically, which is very efficient for tasks like reading from a file and writing to another file.
-
pipe()
method: Automatically manages the flow of data from the source (readable) to the destination (writable).
Advantages of Streams
-
Efficiency: Instead of loading everything into memory, streams process data in chunks, which is ideal for large files or real-time data.
-
Low Memory Usage: Since streams don’t store the entire data set in memory, they’re much more memory efficient, which is particularly useful for server-side applications.
-
Real-Time Data Processing: Streams are useful for real-time data flows like audio, video, or any form of continuous input/output (e.g., HTTP requests/responses).
Real-World Use Cases for Streams
-
File Manipulation: Reading and writing large files without consuming too much memory.
-
Pipes in Networking: Streaming data between servers (e.g., downloading/uploading files).
-
Real-time Communication: Streaming audio/video data, real-time logs, etc.
-
Data Transformation: Compressing or encrypting data on the fly (using transform streams).