Readable Streams: Non-Flowing and Flowing Modes
Explore the differences between non-flowing and flowing modes in Node.js readable streams. Learn how to read data synchronously with the read() method or handle data asynchronously using event listeners, enhancing your control of data flow in stream-based applications.
We'll cover the following...
A Readable stream represents a source of data. In Node.js, it’s implemented using the Readable abstract class, which is available in the stream module.
Reading from a stream
There are two approaches to receive the data from a Readable stream: non-flowing (or paused) and flowing. Let’s analyze these modes in more detail.
The non-flowing mode
The non-flowing (or paused) mode is the default pattern for reading from a Readable stream. It involves attaching a listener to the stream for the readable event, which signals the availability of new data to read. Then, in a loop, we read the data continuously until the internal buffer is emptied. This can be done using the read() method, which synchronously reads from the internal buffer and returns a Buffer object representing the chunk of data. The read() method has the following signature:
readable.read([size])
=Using this ...