Didn’t get NodeJs Buffer and Stream yet? Here’s the best explanation for you.
Learning to nodeJs or learned, but didn’t get the Buffer and Stream yet? I have covered these things in this article. It’s for beginners and intermediate developers.
Prerequisites:
You should have familiarity with Nodejs or any other Back End language.
I won’t take you to more into the depth of these topics. But, will do the best introduction where you will get all in a single shot.
Please see this picture and keep seeing it whenever you need a visual reference.
Stream
The Stream is a sequence of data that is being moved from point A to point B. It is called Stream. For i.e., Inside a pipe where water is moving point A to point B. The data could be anything, an image, a video, or another format of the file.
What if data gets slowly-in and quickly-out or quickly-in and slowly-out? It might work for some cases, but not for all. You can control the flow with available functions. I am not covering that in this article.
Buffer
Buffer is a space or waiting area where your data is stored temporarily; it is often stored in RAM. It is called Buffer. You may have experienced youtube buffering. I will explain it below.
Why this data need to be stored?
Because your device can’t process the whole data at once and note the point that you aren’t going to consume entire data at once.
Didn’t get it?
For i.e., If water is being moved from point A to point B, it might not be moving at the same speed, which could be slow or fast at any point, as I said earlier. It creates a waiting area where water isn’t available yet. That blank space will be filled first, and then you can get the water to point B.
How and why this data is being moved?
Let’s say you want to watch a youtube video. You choose that video and play. Your device will get the data from the server and process it to play it on your screen.
Let’s say, The video file is bigger and holds 80MB file size storage, for example. It will be hard to move this 80MB data from the server to your device in one shot. It will require high-speed internet data and will use a significant amount of memory and CPU load. You don’t need to get all data at once to watch videos because you can’t watch the minutes of video in seconds.
To avoid this, the data is often being moved in chunks, which means small-small parts. These small-small parts move to your video player, and it processes these. This way, You can watch the video with ease without waiting for the whole video to be downloaded or processed. This works the same for other formats of files.
That’s all the Buffer and Stream.
I hope you got it what I have written. Please share this article with your peers if you liked it and want more articles like this.