Different Streams in Node.js

Hello! I hope you had a good day and an awesome week. 😊

In this post, I thought talking about different types of streams in Node.js.

A stream as defined in computer science is a sequence of data elements (usually bytes, but not necessarily) made available over time which can be accessed sequentially.

In Node.js, there are 4 types of streams namely,
  •          Readable             -  stream which is used to read data
  •          Writable              -  stream which is used to write data
  •          Duplex                -  stream which is used to both read and write data
  •          Transform           -  stream which can modify or transform the data as it is being read or written

Each of the above streams are ‘EventEmitter’ instances. That means all the objects which emit events are instances of the ‘EventEmitter’ class. Listed below are some common events emitted by the streams.
  •           data       -  fired when there is data to be read
  •           error      -  fired when there is any error while reading or writing data
  •           end        -  fired when there is no more data to be read
  •           finish     -  fired when all the data is flushed to underlying system


Reading a File (Readable Stream)

I created a text file called ‘text’ and there included a text which says ‘NodeJS stream readable’.
  

In the above code, I have used use strict, the purpose of it can be found in a previous post. In the next line fs inside the brackets means file stream. It has been imported to a constant known as fs. Another constant known as fileName is created to store the path of the file that needs to be read. __dirname is a local variable created by node for each module to store its path starting from the root. This is same as path.dirname(__filename) where __filename contains the absolute path of the module file. With the __dirname, the file that needs to be read is concatenated. Then readFile method is called where we pass the absolute path of the file that needs to be read (which is in the filename constant) and a function. For this function err and data will be passed which will contain errors and data respectively. Then the function will handle data and errors as they are wished to be handled.  We can omit the filename from the function if we have created a readstream giving the filename as the parameter as shown.


The output of the first code will be as follows,


Data is passed through as a bunch of hexadecimal values. If we remove toString() which converts the hexadecimal values to string, we can see the following output,


As we already know Node.js is asynchronous. So, if we want to read the file synchronously, we can use readFileSync method where only the absolute path of the file to be read needs to be passed. Following code will give the same output.



Writing to a File (Writable Stream)

To write to a file it doesn’t necessarily needed to have been created beforehand. Node.js will do that for us.


In the above code, a writestream is created. This is not necessary. If we are not creating a writestream we need to pass the filename as a parameter to the write function. Here what is needed to be written is put into a variable and passed into the function. Again, this is not necessary. It can be directly passed. end() function is used indicate that it is the end of the data flow. finish is used to mark the end of the writing and error is used to handle any errors. Output is as follows,



Reading from and Writing to a File

Node.js provides the ability to write to a file when reading from a file. Below shown is an example,


In the above code, the pipe() function is used to connect the readstream and writestream. In here also it isn’t necessary for the destination file to be created beforehand. on() method is used to confirm whether the code is working properly. The output will be,



Examples for Duplex and Transform streams are socket and zlib respectively. These are a bit advanced streams which will be covered in a later post.

I hope you got an idea about what streams are and how to use them in Node.js.

I wish you all an amazing week ahead! Adios 😊

Comments

Popular posts from this blog

Double Submit Cookies Pattern - Node.js

Synchronizer Token Pattern - Node.js

Implementing an OAuth 2.0 Client - Node.js