Introduction
We require streams in Node Js to handle and manipulate the streaming data like a video, a large file, etc. The streams module in Node.js is used to manage all the streams. A stream is an abstract interface used to work along with the streaming data in Node.js. There are many stream objects,Node.js offers us.
For instance, both are considered stream instances if we request an HTTP server and process.stdout. Streams can be readable, writable, or both. All streams are instances of EventEmitter.
To access the stream module, the syntax to be used is:
const stream = require('stream');
The stream module is used to create new types of stream instances.
Types of Streams in Node.Js
In NodeJs, there are four different types of streams available for us to use. We have four main types of streams available. Let’s see each of them with one-liner explanations.
-
Readable streams → This stream is used to create a stream of data for reading, for example: to read a large chunk of files.
Example:
const fs = require('fs');
const readableStream = fs.createReadStream('./article.md', {
highWaterMark: 10
});
readableStream.on('readable', () => {
process.stdout.write(`[${readableStream.read()}]`);
});
readableStream.on('end', () => {
console.log('DONE');
});
-
Writable streams → This stream is used to create a stream of data to write. For example: to write a large amount of data in a file.
Example:
const fs = require('fs');
const file = fs.createWriteStream('file.txt');
for (let i = 0; i < 10000; i++)
{
file.write('Hello world ' + i);
}
file.end();
-
Duplex streams → This stream is used to create a stream that is both readable and writable at the same time.
Example:
const server = http.createServer((req, res) => {
let body = '';
req.setEncoding('utf8');
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
consol.log(body);
try {
// Send 'Hello World' to the user
res.write('Hello World');
res.end();
} catch (er) {
res.statusCode = 400;
return res.end(`error: ${er.message}`);
}
});
});
-
Transform streams → This stream is used to create a readable and writable stream, but the data in the stream can be modified while reading and writing to the stream.
Example: Creating a transform stream for calculating a value.
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
var Transform = stream.Transform ||
require('readable-stream').Transform;
function codingninjas(options) {
if (!(this instanceof codingninjas)) {
return new codingninjas(options);
}
Transform.call(this, options);
this.digester = crypto.createHash('val1');
}
util.inherits(codingninjas, Transform);
codingninjas.prototype._transform = function (chunk, enc, cb) {
var buffer = (Buffer.isBuffer(chunk)) ?
chunk :
new Buffer(chunk, enc);
this.digester.update(buffer);
cb();
};
codingninjas.prototype._flush = function (cb) {
this.push(this.digester.digest('hex'));
cb();
};
// try it out
var codingninjas = new ShaSum();
codingninjas.pipe(process.stdout); // output to stdout
codingninjas.write('hello world\n'); // input line 1
codingninjas.write('another line'); // input line 2
codingninjas.end(); // finish