#
Streams in Node.js
This tutorial explains to you what is and how you can use a Streams in Node.js.
Streams in Node.js (or in general) are objects that let you read data from a source, transform the data it contains or write data to a destination in continuous fashion.
There are essentially 3 major concepts:
- source - where the data comes from
- pipe - pipe's job is to get the output of one program and pass it as input to another one
- sink - where your data ultimately goes
The 5 classes of streams are:
Readable
Streams : sourcesWritable
Streams : sinksDuplex
Streams : both source and sinkTransform
Streams : in-flight stream operationsPassthrough
Streams : a trivial implementation of a Transform stream that simply passes the input bytes across to the output
Here it is a Readable Stream example:
const { Readable } = require('stream');
const inStream = new Readable({
read(size) {
console.log('size='+size);
console.log('');
}
}).on('data', function(chunk) {
console.log('chunk='+chunk);
});
console.log('');
inStream.push('123456');
inStream.push('AB');
inStream.push('3');
inStream.push(null);
inStream.on('end',function(){
console.log('STREAM ended.');
});
inStream.on('error', function(err){
console.log(err.stack);
});
And here it is the output of that script into the Console:
Remarks:
- On Readable Streams in Node.js you can put data (with push method)
- On Readable Streams in Node.js you can define events "when the stream receives data", "when the stream has an error" or "when the stream ends"
- push(null) ends the stream;
Here it is another Readable Stream example:
const { Readable } = require('stream');
const inStream = new Readable({
read(size) {
console.log('size='+size);
console.log('');
}
}).on('data', function(chunk) {
console.log('chunk='+chunk);
});
console.log('');
inStream.push('123456');
inStream.push('AB');
x = inStream.read();
console.log('x='+x);
y = inStream.read();
console.log('y='+y);
inStream.push('3');
inStream.push(null);
inStream.on('end',function(){
console.log('STREAM ended.');
});
inStream.on('error', function(err){
console.log(err.stack);
});
And here it is the output of that script into the Console:
Remarks:
- On Readable Streams in Node.js you can read data (with read method)
- On Readable Streams in Node.js you can read data as it has been arrived.
- You can add a value as parameter into the read method call in order to get more/ less data from the stream.
Here it is another Readable Stream example:
const { Readable } = require('stream');
const inStream = new Readable({
read(size) {
console.log('size='+size);
console.log('');
}
}).on('data', function(chunk) {
console.log('chunk='+chunk);
});
inStream.addListener('data', chunk1 => {
console.log('fromNewListener='+chunk1);
});
console.log('');
inStream.push('123456');
inStream.push('AB');
inStream.push('3');
inStream.push(null);
inStream.on('end',function(){
console.log('STREAM ended.');
});
inStream.on('error', function(err){
console.log(err.stack);
});
And here it is the output of that script into the Console:
Remark:
On Readable Streams in Node.js you can add/ remove more listeners, during the stream creation or after.
Piping is a mechanism where we provide the output of one stream as the input to another stream. Here it is an example:
const { Readable } = require('stream');
const inStream = new Readable({
}).on('data', function(chunk) {
// Do something if you want ...
});
console.log('');
inStream.push('123456');
inStream.push('AB');
inStream.pipe(process.stdout);
inStream.push('3');
inStream.push(null);
inStream.on('end',function(){
console.log('');
console.log('STREAM ended.');
});
inStream.on('error', function(err){
console.log(err.stack);
});
And here it is the output of that script into the Console:
Remarks:
- the Console is a writable stream as it take an input and put it down on the Console.
- the data from the readable stream is put into the console using a pipe.
Here it is a writable stream in Node.js which receive data from a readable stream (the writable stream put the data into a file at the OS level):
const { Readable } = require('stream');
var fs = require("fs");
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
//Create a Readable Stream
const readableStream = new Readable({
read(size) {
}
}).on('data', function(chunk) {
// Do something if you want ...
});
console.log('');
readableStream.push('123456');
readableStream.push('AB');
readableStream.pipe(writerStream);
readableStream.push('3');
readableStream.push(null);
readableStream.on('end',function(){
console.log('');
console.log('STREAM ended.');
// Mark the end of file
writerStream.end();
});
readableStream.on('error', function(err){
console.log(err.stack);
});
If you look into the output.txt file you will see: 123456AB3.