Monday, July 29, 2019

What is Node.js Streams ?

Streams are the objects that facilitate you to read data from a source and write data to a destination. There are four types of streams in Node.js:
  • Readable: This stream is used for read operations.
  • Writable: This stream is used for write operations.
  • Duplex: This stream can be used for both read and write operations.
  • Transform: It is type of duplex stream where the output is computed according to input.
Each type of stream is an Event emitter instance and throws several events at different times. Following are some commonly used events:
  • Data:This event is fired when there is data available to read.
  • End:This event is fired when there is no more data available to read.
  • Error: This event is fired when there is any error receiving or writing data.
  • Finish:This event is fired when all data has been flushed to underlying system.

Node.js Reading from stream

Create a text file named input.txt having the following content:
  1. Javatpoint is a one of the best online tutorial website to learn different technologies in a very easy and efficient manner.   
Create a JavaScript file named main.js having the following code:
File: main.js
  1. var fs = require("fs");  
  2. var data = '';  
  3. // Create a readable stream  
  4. var readerStream = fs.createReadStream('input.txt');  
  5. // Set the encoding to be utf8.   
  6. readerStream.setEncoding('UTF8');  
  7. // Handle stream events --> data, end, and error  
  8. readerStream.on('data', function(chunk) {  
  9.    data += chunk;  
  10. });  
  11. readerStream.on('end',function(){  
  12.    console.log(data);  
  13. });  
  14. readerStream.on('error', function(err){  
  15.    console.log(err.stack);  
  16. });  
  17. console.log("Program Ended");  
Now, open the Node.js command prompt and run the main.js
  1. node main.js  
Output:
Node.js streams 1

Node.js Writing to stream

Create a JavaScript file named main.js having the following code:
File: main.js
  1. var fs = require("fs");  
  2. var data = 'A Solution of all Technology';  
  3. // Create a writable stream  
  4. var writerStream = fs.createWriteStream('output.txt');  
  5. // Write the data to stream with encoding to be utf8  
  6. writerStream.write(data,'UTF8');  
  7. // Mark the end of file  
  8. writerStream.end();  
  9. // Handle stream events --> finish, and error  
  10. writerStream.on('finish', function() {  
  11.     console.log("Write completed.");  
  12. });  
  13. writerStream.on('error', function(err){  
  14.    console.log(err.stack);  
  15. });  
  16. console.log("Program Ended");  
Now open the Node.js command prompt and run the main.js
  1. node main.js  
You will see the following result:
Node.js streams 2
Now, you can see that a text file named "output.txt" is created where you had saved "input.txt" and "main.js" file. In my case, it is on desktop.
Open the "output.txt" and you will see the following content.
Node.js streams 3

Node.js Piping Streams

Piping is a mechanism where output of one stream is used as input to another stream. There is no limit on piping operation.
Let's take a piping example for reading from one file and writing it to another file.
File: main.js
  1. var fs = require("fs");  
  2. // Create a readable stream  
  3. var readerStream = fs.createReadStream('input.txt');  
  4. // Create a writable stream  
  5. var writerStream = fs.createWriteStream('output.txt');  
  6. // Pipe the read and write operations  
  7. // read input.txt and write data to output.txt  
  8. readerStream.pipe(writerStream);  
  9. console.log("Program Ended");   
Open the Node.js and run the mian.js
  1. node main.js  
Node.js streams 4
Now, you can see that a text file named "output.txt" is created where you had saved ?main.js? file. In my case, it is on desktop.
Open the "output.txt" and you will see the following content.
Node.js streams 5

Node.js Chaining Streams

Chaining stream is a mechanism of creating a chain of multiple stream operations by connecting output of one stream to another stream. It is generally used with piping operation.
Let's take an example of piping and chaining to compress a file and then decompress the same file.
File: main.js
  1. var fs = require("fs");  
  2. var zlib = require('zlib');  
  3. // Compress the file input.txt to input.txt.gz  
  4. fs.createReadStream('input.txt')  
  5.   .pipe(zlib.createGzip())  
  6.   .pipe(fs.createWriteStream('input.txt.gz'));  
  7.   console.log("File Compressed.");  
Open the Node.js command prompt and run main.js
  1. node main.js  
You will get the following result:
Node.js streams 6
Now you will see that file "input.txt" is compressed and a new file is created named "input.txt.gz" in the current file.
To Decompress the same file: put the following code in the js file "main.js"
File: main.js
  1. var fs = require("fs");  
  2. var zlib = require('zlib');  
  3. // Decompress the file input.txt.gz to input.txt  
  4. fs.createReadStream('input.txt.gz')  
  5.   .pipe(zlib.createGunzip())  
  6.   .pipe(fs.createWriteStream('input.txt'));  
  7.   console.log("File Decompressed.");  
Open the Node.js command prompt and run main.js
  1. node main.js  
Node.js streams 7

1 comment: