# Streams in Node.js

In 
Node
Published 2022-12-03

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:

  1. source - where the data comes from
  2. pipe - pipe's job is to get the output of one program and pass it as input to another one
  3. sink - where your data ultimately goes

The 5 classes of streams are:

  • Readable Streams : sources
  • Writable Streams : sinks
  • Duplex Streams : both source and sink
  • Transform Streams : in-flight stream operations
  • Passthrough 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:

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:

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:

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:

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.