Open In App

What is stream and its types in Node.js ?

A stream is a way of data handling that helps us to obtain a sequential output by reading or writing the input (files, network communications, and any kind of end-to-end information exchange). That is, they let you read data from a source or write it to a destination or perform any other specific task uninterruptedly and constantly. The stream is not a unique concept to Node.js and it is a part of Unix for quite a long time. A pipe operator is used to make the programs react with each other by passing streams. Hence, the Node.js stream is used as a basis for all streaming APIs. 

Example: When you are streaming YouTube, Netflix, or Spotify then, instead of the whole content downloading all at once, it downloads in small chunks while you keep browsing. Another example can be chatting on Facebook or WhatsApp where the data is continuously flowing between two people. This is because instead of reading all the data at once in the memory the stream processes it into smaller pieces to make large files easily readable. It is useful because some files are larger than the available free space that you have on your device. Hence, the stream makes such files readable. 



Advantages of Stream:

Types of Stream: 



Different operations in a stream are:




const fs = require("fs");
let data = '';
 
// Create a readable stream
const readerStream = fs.createReadStream("input.txt");
// Set the encoding to be utf8.
readerStream.setEncoding("UTF8");
 
// Handling data stream event
readerStream.on("data", function(chunk) {
    data += chunk;
});
 
// Handling end stream event
readerStream.on("end",function() {
    console.log(data);
});
 
// Handling error stream event
readerStream.on("error", function(err) {
    console.log(err.stack);
});

Run the main.js file with the following command:

$ node main.js

The output of the above command is shown below:

This is a code to learn about the reading from a stream.

Writing to a stream Filename: main.js 




const fs = require('fs');
let data = 'This is a code to learn"
            + " about writing in a stream.';
 
// Create a writable stream
let 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();
 
// Handling finish stream event
writerStream.on('finish', function () {
});
 
// Handling error stream event
writerStream.on('error', function (err) {
    console.log(err.stack);
});

Run the main.js file with the following command:

$ node main.js

After executing the above command, a file named output.txt will be created in the current directory with the following text:

This is a code to learn about writing in a stream.
This is a code to learn about piping the stream.

Filename: main.js 




const fs = require('fs');
 
// Create a readable stream
const readerStream = fs.createReadStream('input.txt');
 
// Create a writable stream
const writerStream = fs.createWriteStream('output.txt');
 
// Pipe the read and write operations
// read input.txt and write data to output.txt
readerStream.pipe(writerStream);

Run the main.js file with the following command:

$ node main.js

After executing the above command, a file named output.txt will be created in the current directory with the following text:

This is a code to learn about piping the stream.

Filename: main.js 




const fs = require('fs');
const zlib = require('zlib');
 
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
    .pipe(zlib.createGzip())
    .pipe(fs.createWriteStream('input.txt.gz'));
 
console.log('File Compressed.');

Run the main.js file with the following command:

$ node main.js

The output of the above command is shown below:

File Compressed.

You will find that input.txt has been compressed and it created a file input.txt.gz in the current directory. Now code to decompress the above-created file is shown below: Filename: main.js 




const fs = require('fs');
const zlib = require('zlib');
 
// Decompress the file input.txt.gz to input.txt
fs.createReadStream('input.txt.gz')
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriteStream('input.txt'));
 
console.log('File Decompressed.');

Run the main.js file with the following command:

$ node main.js

The output of the above command is shown below:

File Decompressed.

You will find that input.txt.gz has been decompressed.


Article Tags :