Streams in Node JS

What are streams in Node JS?

Streams in Node are objects that allow to read and write data. Of course, to read the data, the stream requires some source of data, and to write the data, it needs some destination.

Streams perform the read operation from source and write operation on the destination in a continuous manner.

There are four types of streams in Node JS.

  1. Writeable Streams: In writable streams, we can write the data. For example, the fs.createWriteStream deals with files and allows us to write in the file using streams.
  2. Readable Streams: These streams allows us to read the data from them in a continous manner. For example, fs.createReadStream allows us to read the contents of a file.
  3. Duplex Streams: As the name suggest, these streams allows us to perform both read and write operations using streams. For example, net.socket stream support both reading and writing the data over the sockets.
  4. Transform Streams: These streams are more complex but perform magical operations with the data to be written or read. These are capable of modifying or transforming the data. The best example to understand transform streams is the compressed files. The data is compressed when it is written in the file and decompressed when read from it.

All of the above streams throw EventEmitter instance and in addition to these, many other events are based on time events. For instance, the following are some commonly used events of the streams.

  • data – This event is emitted by the Stream when there is any data available in the stream and it is readable.
  • end – This event is emitted when there is no data in the stream to read.
  • error – When the stream is unable to perform read or write operation with its datam it throws error event.
  • finish – This event is emitted when the data read or write is completed by the stream.

In the next section, we will discuss some commonly used operations of the streams.

How to read data from the Stream?

Create a text file and put some sample text in it. We will be using this file with the read stream.

Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!

Now, create a file and name it main.js. Put the following code snippet in the file.

var fs = require("fs");
var data = '';

// Create a readable stream
var readerStream = fs.createReadStream('input.txt');

// Set the encoding to be utf8. 
readerStream.setEncoding('UTF8');

// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
   data += chunk;
});

readerStream.on('end',function() {
   console.log(data);
});

readerStream.on('error', function(err) {
   console.log(err.stack);
});

console.log("Program Ended");

In the above code, we get the readable stream from the fs object and set the encoding to utf-8. Them we use the data, end and error events of the stream. These are given in the above section.

It will produce the following output in the console.

Program Ended
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!

How to write data into Stream in Node JS?

Create a file and name it main.js. Put the following code in it.

var fs = require("fs");
var data = 'Simply Easy Learning';

// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');

// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');

// Mark the end of file
writerStream.end();

// Handle stream events --> finish, and error
writerStream.on('finish', function() {
   console.log("Write completed.");
});

writerStream.on('error', function(err) {
   console.log(err.stack);
});

console.log("Program Ended");

In the above example code, we use the createWriteStream from the fs object and set the encoding scheme to utf8.

We use the finish and error events of the writable stream. This code will produce the following output in the console.

Program Ended
Write completed.

A file will be created, open it, and verify that it contains the following content.

Simply Easy Learning

What is piping the streams in Node JS?

Piping is a beautiful use case of streams in Node JS. In piping, we make the output of a stream as the input of another stream. For instance, if you want to transfer the contents of a file to another file, you can make use of the piping concept. Open the file containing the data by reading stream and feeding this output to the write stream of another file.

Create a file main.js with the following code and understand the piping concept.

var fs = require("fs");

// Create a readable stream
var readerStream = fs.createReadStream('input.txt');

// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');

// Pipe the read and write operations
// read input.txt and write data to output.txt
readerStream.pipe(writerStream);

console.log("Program Ended");

It will produce the following output in the console.

Program Ended

After the execution of the above code, there will be a file output.txt which will contain the following content.

Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!

Reference to the official documentation of Streams in Node JS

Scroll to Top