Streams
Streams
What are Streams?
Streams are objects that let you read data from a source or write data to a destination in continous fashion. In Node.js, there are four types of streams.
- Readable - Stream which is used for read operation.
- Writable - Stream which is used for write operation.
- Duplex - Stream which can be used for both read and write operation.
- Transform - A type of duplex stream where the output is computed based on input.
Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are:
- data - This event is fired when there is data is available to read.
- end - This event is fired when there is no more data 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
This tutorial will give you understanding on commonly used operations on Streams.
Reading from stream
Create a text file named input.txt having following content
Fastread is giving self learning content to teach the world in simple and easy way!!!!!
Create a js file named main.js which has the following code:
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");
Now run the main.js to see the result:
$ node main.js
Verify the Output
Program Ended Fastread is giving self learning content to teach the world in simple and easy way!!!!!
Writing to stream
Create a js file named main.js which has the following code:
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");
Now run the main.js to see the result:
$ node main.js
Verify the Output
Program Ended Write completed.
Now open output.txt created in your current directory and verify the following content available in output.txt file.
Simply Easy Learning
Piping streams
Piping is a mechanism where we provide output of one stream as the input to another stream. It is normally used to get data from one stream and to pass output of that stream to another stream. There is no limit on piping operations. Now we'll show a piping example for reading from one file and writing it to another file.
Create a js file named main.js which has the following code:
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");
Now run the main.js to see the result:
$ node main.js
Verify the Output
Program Ended
Open output.txt created in your current directory and verify the following content available in output.txt file.
Fastread is giving self learning content to teach the world in simple and easy way!!!!!
Chaining streams
Chanining is a mechanism to connect output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we'll use the piping and chaining to first compress a file and then decompress the same.
Create a js file named main.js which has the following code:
var fs = require("fs"); var 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.");
Now run the main.js to see the result:
$ node main.js
Verify the Output
File Compressed.
You will find that input.txt file has been compressed and it created a file input.txt.gz in the current directory. Now let's try to decompress the same file using the following code.
var fs = require("fs"); var 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.");
Now run the main.js to see the result:
$ node main.js
Verify the Output
File Decompressed.