Software Developer
JavaScript move permits to programmatically get the right of entry to a circulation of facts obtained on the community. Streaming is to interrupt the resource or facts into smaller chunks, and processing is done little by little. In javascript movement, the entire collection is loaded into memory after which processed. For this, we don’t need tons of memory to maintain these bits of facts and their result after the transformation. Previously, while we desired to procedure records along with text or a video, they had to be downloaded and deserialized to a required layout before processing. Now due to javascript circulate, uncooked facts obtained can be at once processed little by little.
There is no particular syntax similar to a function or a JavaScript method as JavaScript Stream is an API that allows us to work with the data.
Have 2 types of streaming modes:
Streams in NodeJS are readable and writable, known as Duplex Stream. This is because javaScript streams are Versatile, so these Streams’ main purpose is to reduce the memory needed.
We will be running all the JavaScript applications on the NodeJS platform; NodeJS is written in JavaScript Language, so there would be no change syntactically. Install VSCode and NodeJS to run these below examples.
Given below are the examples of JavaScript Stream:
Here we shall see how to create a JS file and read the data from the data.txt file.
Stream.js:
var fs = require("fs");
var data = '';
var readStream = fs.createReadStream('data.txt');
readStream.setEncoding('UTF8');
readStream.on('data', function(bits) {
data += bits;
});
readStream.on('end',function() {
console.log("Data inside data.txt file is: ", data);
});
readStream.on('error', function(err) {
console.log(err.stack);
});
console.log("JavaScript Program ends here!!");
Here in EduCBA, you can
learn real world sills
online, at your time,
your place and on any device
Here we shall see WritableStream write the given data onto a .txt file.
var fs = require("fs");
var data = 'eduCBA provides many diversified courses at minimal rate, Friendly instructors and professional courses';
var writeStream = fs.createWriteStream('dataOP.txt');
writeStream.write(data,'UTF8');
writeStream.end();
writeStream.on('finish', function() {
console.log("Write to dataOP.txt file has been completed successfully! Please open dataOP.txt file");
});
writeStream.on('error', function(err){
console.log(err.stack);
});
console.log("Program has been completed");
var fs = require("fs");
var readStream = fs.createReadStream('data.txt');
var writeStream = fs.createWriteStream('dataOP.txt');
readStream.pipe(writeStream);
console.log("Data from data.txt has been copied to dataOP.txt successfully");
var fs = require("fs");
var zlib = require('zlib');
fs.createReadStream('data.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('data.txt.gz'));
console.log("File has been Compressed to Gzip.");
var fs = require("fs");
var zlib = require('zlib');
fs.createReadStream('data.txt.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('data.txt'));
console.log("File has been Decompressed to .txt.");
Given below are the advantages mentioned:
We have a visible javascript flow and its syntax. The way it works programmatically with some examples on readable stream, writable stream, piping stream, and chaining streams is illustrated. Also, see some of the blessings which are indexed above. Despite such a lot of different environments to be had, javascript streams are one of the reasons why humans had been using NodeJS.