Search This Blog

2023/04/13

Node.js:Stream & Buffer

Streams are objects that let you read data from a source or write data to a destination in continuous fashion.

Types of streams

There are four fundamental stream types within Node.js:

    • Writable: streams to which data can be written (for example, fs.createWriteStream()).

    • Readable: streams from which data can be read (for example, fs.createReadStream()).

    • Duplex: streams that are both Readable and Writable (for example, net.Socket).

    • Transform: Duplex streams that can modify or transform the data as it is written and read (for example, zlib.createDeflate()).


There are two types of readable streams:

    • Flowing stream — A stream that keeps on passing the data that can be directly listened to by using the data event on the stream.

    • Non-flowing stream — A stream that does not push data automatically. Instead, the stream stores the data in the buffer and we need to explicitly call the read() method of the stream to read it.

Node.js streams extend the EventEmitter class. We can listen to events like data and end in streams.

 buffer is a temporary memory that a stream takes to hold some data until it is consumed. buffer removes the data after being read by the consumer.


No comments:

Post a Comment