The read stream is a part of the streaming data handling system in Node.js that is specifically responsible for reading data from a source. Imagine you have a large reservoir of data. A read stream enables your application to consume this data one sip at a time, instead of gulping it down all at once. It is particularly useful when working with large files or streaming data in real-time. In this topic, you'll take a deep dive into read streams, understand how to create them and learn about the events associated with the stream. Finally, you will learn to control their flow for efficient data handling.
Understanding events in read stream
A read stream is an instance of the EventEmitter class. It emits several events at various stages of reading data. The most commonly used events are:
data— This event is fired when data is available for the stream to read;end— This event is fired when there is no more data to read;error— This event is fired when an error occurs while receiving or writing data;close— This event is fired when the stream and any of its underlying resources, for example — a file, are closed.
Here is an example of how you can use these events:
// input.txt
Hello from text file
// readStreamEventsExample.js
const fs = require('fs');
const readStream = fs.createReadStream('input.txt');
readStream.on('data', function(chunk) {
console.log('Received %d bytes of data.', chunk.length);
});
readStream.on('end', function(){
console.log('There is no more data to read.');
});
readStream.on('error', function(err){
console.error('An error occurred:', err);
});
readStream.on('close', function(){
console.log('Stream closed.');
});
/**
* Console Output:
*
* Received 21 bytes of data.
* There is no more data to read.
* Stream closed.
*/
In this code snippet, we set up a read stream with fs.createReadStream('input.txt'), after which we attach various event listeners using the on method. Then it listens for data, end, error, and close events and logs appropriate messages to the console for each case.
Controlling the flow
While the read stream is an excellent tool for data handling, there may be times when you wish to control how quickly data flows through your stream. Node.js provides two methods to achieve this:
readStream.pause(): Halts the flow of data temporarily. It is like pressing the pause button on your data stream.readStream.resume(): After you've paused the flow, you can use this method to resume the data stream.
In addition to these flow control methods, the pipe() function is another useful feature. It essentially pipes data from a read stream directly into a write stream. The pipe() function provides an easy way to transport data from source to destination.
Let's look at an example showcasing these methods:
// readStreamFlowControlExample.js
const fs = require('fs');
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');
readStream.on('data', function(chunk) {
console.log('Received %d bytes of data.', chunk.length);
readStream.pause();
console.log('There will be no additional data for 1 second.');
setTimeout(function(){
console.log('Now data will start flowing again.');
readStream.resume();
}, 1000);
});
// Pipe the read stream to the write stream
readStream.pipe(writeStream);
The code snippet shown above consists of a read stream and a write stream. It listens for the data event. After each chunk of data is received, the stream is paused for a second before resuming. Finally, pipe() directs data from the read stream into the write stream. This process continues until all the data is read and written.
Conclusion
In Node.js, a read stream is a useful tool to read data piece by piece, rather than all at once. This is great when dealing with huge amounts of data or information that arrives over time like live updates.
A read stream in Node.js also acts like a messenger. It keeps you informed about what's happening as it reads data. It tells you when it has some data to give (data event), when all the data has been read (end event), if something went wrong (error event), and when reading is complete and closed (close event). Sometimes, you may want to read data at a slower pace. For this, Node.js provides pause() and resume() methods. It's like having a remote control for the stream of data!
On top of all this, Node.js comes with a handy tool called pipe(). It allows you to directly transfer data from a read stream into a write stream, like pouring water from one glass into another. In short, read streams in Node.js make working with data much more manageable and efficient.