Everything You Need to Know About Streams in Node.js

Streams in Node.js are infamous for being hard to work with and even harder to understand. This article will make it easier for you to understand streams, latest trends in Node JS and how to work with it.

What are Streams?

Streams are a collection of data just like arrays or strings. However, streams might not be available all at once. What makes them really powerful when working with large amounts of data is that they process the content piece by piece without keeping it all in the memory.

One of the best real-life examples would be streaming services like Netflix and YouTube. These websites never ask you to download the video at once, instead, they send the video in chunks, allowing you to watch the video continuously.

Streams are used to read or write input into output sequentially. They can also handle data coming from an external source one chunk at a time. Besides, they give you the power of composability in your code.

In a nutshell, streams can be used to handle reading/writing files, network communications, or any kind of end-to-end information exchange efficiently.

Why Use Streams?

There are two major advantages of using Streams compared to other data handling methods.

  • First, you don’t need to load large amounts of data to process it.
  • Second, it takes less time to start processing the data as you won’t have to wait until the entire payload has been transmitted.

Types of Streams

  • Writable: These are the streams that allow us to write data.
  • Readable: These are the streams from which data can be read.
  • Duplex: These are both readable and writable.
  • Transform: These are the streams that can modify or transform the data as it is read and written.

The request moduleis a readable stream while the response module is writable steam. The fs module allows you to work with both readable and writable streams.

If you use Express you are more or less using streams to interact with the client. Since TCP sockets, TLS stack, and other connections are based on Node.js streams, they are also used in database connection drivers.

Creating a Readable Stream

The first step to creating a readable stream is initializing it.

const Stream = require(‘stream’)

constreadableStream = new Stream.Readable()

The next step would be sending data to it.



async iterator

When working with streams, it is recommended to use an asynchronous iterator. The asynchronous iterator is a protocol for retrieving the contents of a data asynchronously. In simple terms, the current task is paused before retrieving an item.

Ensure that you don’t mix async functions with EventEmitterbecause as of now, there’s no way to catch a rejection when it is emitted within an event handler. This, in turn, makes it hard to track bugs and memory leaks.

Therefore, the best way is to always wrap the content in a try/catch block and handle errors. The pull request aims to solve this issue once it lands on the Node core.

Types of Reading Modes

The 2 modes of readable streams are 1) Flowing 2) Paused

Regardless of whether a stream is in flowing mode or paused mode, it can be in an object mode.

Flowing Mode: In this mode, the data is read from the underlying system automatically using events via the EventEmitter interface.

Paused Mode: In this mode, the method must be called explicitly to read chunks of data from the stream.

Creating a Writable Stream

In order to create a writable stream, you need to call write() on the stream instance. It would look like this.

In the end, call writable.end() to indicate that no more data will be written to the Writable.


Piping is a tool in which you provide the output of one stream as the input to another. Piping is generally used to process streamed data in multiple steps. Also, ensure that you use pipeline and not pipe. 

The Stream Module

The Node.js stream module provides the groundwork upon which all the streaming APIs are made. The stream is an example of the EventEmitter class which manages events asynchronously in Node.js.

After all, Streams are not that hard (pun intended). Streams and pipeline are the most robust features in Node.js Streams.

Author Bio

Bharat Patel, who heads the digital marketing team at BrainvireInfotech, is armed with over 12 years of experience in the fields of online marketing and project management. He is extremely proactive in implementing the latest technological innovations in his projects. Bharat’s core expertise lies in search engine optimization (SEO), social media marketing, and conversion rate optimization, among other things. His immense flare of writing encourages him to consistently pen down words revolving around current trends and innovations that relate to his fields of interest.



Children’s author Jo Kemp believes not. When she began her career in Manchester and wrote the original scripts for Chorlton and the Wheelies and Jamie and the Magic Torch  in the late 70’s she adopted a gentle, narrative style with little regard for political correctness and both series attracted a cult following. She continued in this style when she wrote Boodle Books published by Thurman who were responsible for Mr. Men.

Fast and furious programmes then took the market and the more traditional approach to storytelling took a back seat other than timeless classics and well-marketed formulaic concepts, but there was always a need for something different.

Boodle Books  had a temporary reprise when they were re-launched as  Hoo Ha House but the time and circumstances still weren’t right. Together with the Writers’ Guild, Jo reclaimed the rights along with some 23,000 residual books which she distributed at her own cost free of charge to local schools, hospitals and Barnardos.

At her son Luke’s insistence, due to his belief in the stories he’d grown up with, he convinced Jo not to leave it there and that the best vehicle to get the stories out would be via Social Media.

Crowdfunding was a viable option for raising capital and so rather than suffer the loss of editorial control they decided they had the combined knowledge and expertise to produce/market the project themselves and by utilising  KickStarter, funding could be available.

However, backers would need proof of concept meaning the first five films would have to be self-funded and having run her own film production company employing at one point, around 30 people to produce over a hundred episodes of Raggy Dolls, Jo was understandably skeptical.

However, Luke’s tenacity convinced her that with the advances in technology he could shelve his job as a freelance web developer and devote himself entirely to the project.

Luke directs and animates, Jo found a new vocation as Voice Over and the first five films with associated promotion were completed in under six months. BoodleBobs, with ninety or more characters will also appear in a lineup of songs across the 26 stories. It’s all original content that isn’t dumbed down and is written to also appeal to adults.

After all the noise there’s a growing demand for quiet, one-to-one traditional storytelling that extols these old-fashioned values while embracing new media.

Five of the twenty six books/videos plus an animated signature-music video are on the BoodleBobs YouTube Channel .  And a free Ebook is available from the BoodleBobs website. Work has already started on the next five stories so it’s now a matter of time to see if this mother and son duo can capture their audience and win support for BoodleBobs.


The Times (September 24 2005)

 Kickstarter Project



HooHa House List of Books

Free E-Book Download

More Original Content (Songs)