Streams In Node.JS

Ewuzie Joan
2 min readSep 5, 2023

--

Streams are one of the fundamental concepts that power Node. js applications. They are data-handling method and are a way to handle reading/writing files. They’re meant to be like pipes, allowing us to hook up a data source one end, any number of consumers on the other end, and pass data between them.

Streams are collections of data — just like arrays or strings. The difference is that streams might not be available all at once, and they don’t have to fit in memory. This makes streams really powerful when working with large amounts of data, or data that’s coming from an external source one chunk at a time.

Let’s take a “streaming” services such as YouTube, Netflix or AnimePahe for example: these services don’t make you download the video and audio feed all at once. Instead, your browser receives the video as a continuous flow of chunks, allowing the recipients to start watching and/or listening almost immediately. Using streams to process smaller chunks of data, makes it possible to read larger files.

With these said and done… in very simple words and defintion; Streams are getting small pieces or chunks of data from a large data source to the receiving end without waiting for the whole memory to be processed.

Why streams?

Streams basically provide two major advantages compared to other data handling methods:

  1. Memory efficiency: you don’t need to load large amounts of data in memory before you are able to process it
  2. Time efficiency: it takes significantly less time to start processing data as soon as you have it, rather than having to wait with processing until the entire payload has been transmitted

Stream Types.

There are four fundamental stream types in Node.js: Readable, Writable, Duplex, and Transform streams.

  • A readable stream is an abstraction for a source from which data can be consumed. An example of that is the fs.createReadStream method. i.e; streams from which data can be read. lets us read the contents of a file.
  • A writable stream is an abstraction for a destination to which data can be written. An example of that is the fs.createWriteStream method. i.e; streams to which we can write data. lets us write data to a file using streams.
  • A duplex streams is both Readable and Writable. An example of that is a TCP socket.
  • A transform stream is basically a duplex stream that can be used to modify or transform the data as it is written and read. In the instance of file-compression, you can write compressed data and read decompressed data to and from a file. An example of that is the zlib.createGzip stream to compress the data using gzip. You can think of a transform stream as a function where the input is the writable stream part and the output is readable stream part. You might also hear transform streams referred to as “through streams. :

--

--