If you enjoy this story, feel free to give it a like or share it with others who might find it helpful!
I’m at a beach, a place where the ocean meets the land, and I have two different ways to enjoy the waves. In one scenario, I’m standing right at the edge of the water. The waves continuously lap at my feet, one after another, without me having to do anything. This is like the flowing mode in a readable stream. The data, much like the ocean waves, comes at me automatically, and I can choose to interact with it—like jumping or dancing around—but it’s going to keep coming no matter what. The stream is constantly in motion, delivering data as quickly as it can.
Now, I decide to move up the beach a bit, far enough that the waves can’t reach me unless I want them to. I stand with a bucket, carefully choosing when to run down to the ocean, scoop up some water, and run back to my spot. This is the paused mode. I’m in control, deciding exactly when and how much water I gather, much like I can control the flow of data. I can take my time, process each bucketful at my leisure, and only interact with the ocean when I’m ready.
In both modes, I’m interacting with the ocean, but the experience is quite different. Sometimes I want the thrill and spontaneity of the waves rushing in, and other times I prefer the control of my bucket runs. Similarly, with readable streams, I can choose between the constant flow of data in flowing mode or the deliberate, controlled approach of paused mode. Each has its own pace and charm, and knowing how to switch between them lets me enjoy the stream—or the ocean—just the way I want.
Flowing Mode
I’m back at the edge of the water, where the waves continuously lap at my feet. This is analogous to enabling flowing mode in a readable stream. In JavaScript, when a stream is in flowing mode, data is read and emitted automatically as soon as it is available. Here’s how it looks in code:
const fs = require('fs');
// Create a readable stream
const readableStream = fs.createReadStream('example.txt');
// Switch to flowing mode by adding a 'data' event listener
readableStream.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
By attaching a data
event listener, the stream starts flowing automatically, and chunks of data are pushed to the listener as they become available. It’s like the waves coming in continuously.
Paused Mode
Now, imagine I’m standing further up the beach with my bucket, deciding when to go to the water. In JavaScript, paused mode is when the stream waits for me to explicitly request data. Here’s how to handle paused mode:
const fs = require('fs');
// Create a readable stream
const readableStream = fs.createReadStream('example.txt');
// Initially, the stream is in paused mode
readableStream.on('readable', () => {
let chunk;
while (null !== (chunk = readableStream.read())) {
console.log(`Received ${chunk.length} bytes of data.`);
}
});
In paused mode, I have to explicitly call .read()
to get chunks of data, much like choosing when to fill my bucket with water. This allows me greater control over the flow of data processing.
Key Takeaways
- Flowing Mode: Automatically reads data as it becomes available. This is useful for real-time data processing where you want to handle data as it arrives.
- Paused Mode: Requires explicit calls to read data, giving you more control over when and how much data you process at a time.