Node JS - Duplex Stream


Stream is a continuous flow of data between client and server. And the main advantage of using Stream is it does not load the entire data into memory in one go. Instead it divides the data into multiple smaller parts called chunks.


And transmits chunk one at a time. This reduce the huge impact or load that can server might face and perform the server optimization on handling large data transmission


Duplex transmission is a type of stream, which has the ability to perform both Read and Write operation on stream data.

To read more about stream , refer https://motionknowledge.blogspot.com/2021/07/node-js-streams.html

One of the type of Duplex Stream is Pass Through


Example :

const { PassThrough } = require("stream");

const report = new PassThrough();

report.on("data", (chunk=> {

  let count = 0;
  count += chunk.toString().split(" ").length;
  console.log(`Word count :  ${count}`);
});

process.stdin.pipe(report).pipe(process.stdout);