Streams and Buffers
Streams and Buffers
Streams are collection of data, they wouldn’t be available all at once as they
may not fit into the memory, streams are used when working with the large
amounts of data, we need to process one chunk at a time.
stream-demo.js
Output:
We can also pause the stream & resume, let us use some counters and
timers to do that
Output: You see some more chunks after 3s
Streams 101
There are four types of streams.
1. Readable streams: fs.createReadStream() to read the streams
2. Writable streams: fs.createWriteStream() is to write the streams
3. Duplex streams: Can read and write streams, tcp socket is an example
of Duplex streams
4. Transform streams: When you read & write the streams you want to
modify while reading & writing, example of transform is the
compressing of data streams, like zip files
Buffers
A buffer is way to store and manipulate binary data, usually when the data’s
are passed over the network in the binary form we need Buffer to manipulate
store and manipulate them, it could be binary streams or authorization
headers having base64 data.