

{"id":102271,"date":"2021-11-05T09:00:55","date_gmt":"2021-11-05T03:30:55","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=102271"},"modified":"2021-11-06T11:20:36","modified_gmt":"2021-11-06T05:50:36","slug":"streams-in-nodejs","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/","title":{"rendered":"Streams in Nodejs"},"content":{"rendered":"<p>In this article, we will be discussing streams, its advantages, its types, reading streams, writing streams, piping and also chaining.<\/p>\n<h3>What is a stream in Nodejs?<\/h3>\n<p>Streams are objects that we use to read data from a source or write data to a destination in sequential fashion.<\/p>\n<h3>Advantages of Nodejs Stream:<\/h3>\n<p>1. Memory efficiency: It is memory efficient because it enables us to download files in smaller chunks instead of a whole in the memory before we can use it, thus saving space.<\/p>\n<p>2. Time efficiency: We start processing the data in smaller chunks so the procedure starts early .<\/p>\n<p>3. Composable data: Data is composed because of piping.<\/p>\n<h3>Nodejs Stream Types:<\/h3>\n<p>1. <strong>Readable stream:<\/strong> Stream for receiving and reading the data in an ordered manner.<\/p>\n<p>2.<strong> Writable stream:<\/strong> Stream for sending data in an ordered manner.<br \/>\nDuplex stream: It is both readable and writable. We can send in and receive data together.<\/p>\n<p>3. <strong>Transform stream:<\/strong> It is used for modifying the data. It is duplex in nature.<\/p>\n<h3>Operation on streams in Nodejs:<\/h3>\n<p>Below are the different types of operations on streams with examples:<\/p>\n<h4>1. Reading from Nodejs streams:<\/h4>\n<p>We have a text file named \u201cDataFlair.txt\u201d which contains the string \u201cWelcome to DataFlair\u201d. Now in the below code we will be reading this file in small chunks and displaying them:<\/p>\n<p><strong>Code for Reading from stream:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require(\"fs\");\r\nvar data = '';\r\n \r\nvar readerStream = fs.createReadStream(\"DataFlair.txt\");\r\nreaderStream.setEncoding(\"UTF8\");\r\nreaderStream.on(\"data\", function (chunk) {\r\n    data += chunk;\r\n});\r\nreaderStream.on(\"end\", function () {\r\n    console.log(data);\r\n});\r\nreaderStream.on(\"error\", function (err) {\r\n    console.log(err.stack);\r\n});\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-readstream-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103780\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-readstream-output.webp\" alt=\"nodejs readstream output\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-readstream-output.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-readstream-output-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Paused and flow modes in readable stream:<\/h3>\n<p>Readable streams by default start in paused mode but they can be switched to flow and back to paused mode. The switch happens automatically.<\/p>\n<p>To manually switch between the two streams we can use the resume() and pause() methods.<\/p>\n<h4>2. Writing to a Nodejs stream:<\/h4>\n<p>We will be using createWriteStream(\u201cfilename\u201d) from the filesystem module to create a writing stream.<\/p>\n<p><strong>Code for writing to a stream:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nvar data = 'Data\"+ \"Flair';\r\n \r\nvar writerStream = fs.createWriteStream('DataFlair_output.txt');\r\n \r\nwriterStream.write(data, 'UTF8');\r\n \r\nwriterStream.end();\r\nwriterStream.on('finish', function () {\r\n});\r\n \r\nwriterStream.on('error', function (err) {\r\n    console.log(err.stack);\r\n});\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>\u201cWelcome to DataFlair\u201d<br \/>\nYou will see a DataFlair_Output.txt file in your current directory.<\/p>\n<h4>3. Piping in Nodejs:<\/h4>\n<p>In piping, we provide a readable stream from a source as an input to another writable stream which is our destination.<\/p>\n<p><strong>Code for piping:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nvar readerStream = fs.createReadStream('DataFlair.txt');\r\nvar writerStream = fs.createWriteStream('output.txt');\r\nreaderStream.pipe(writerStream);\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>\u201cWelcome To DataFlair\u201d<\/p>\n<p>In your current directory you will see a output.txt file which will contain the data present in the DataFlair.txt file<\/p>\n<h4>4. Chaining Nodejs Stream:<\/h4>\n<p>It means connecting the output of one stream with another stream. It is used with piping operations. We will be compressing the \u201cDataFlair\u201d file and decompressing it using piping for demonstrating chaining of streams.<\/p>\n<p><strong>Code for compressing:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nvar zlib = require('zlib');\r\nfs.createReadStream('DataFlair.txt')\r\n    .pipe(zlib.createGzip())\r\n    .pipe(fs.createWriteStream('DataFlair.txt.gz'));\r\n \r\nconsole.log('File Compressed.');\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>File Compressed<\/p>\n<p>You will see a DataFlair.txt.gz file in your current directory.<\/p>\n<p><strong>Code for Decompressing:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nvar zlib = require('zlib');\r\nfs.createReadStream('DataFlair.txt.gz')\r\n    .pipe(zlib.createGunzip())\r\n    .pipe(fs.createWriteStream('DataFlair.txt'));\r\n \r\nconsole.log('File Decompressed.');\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p>\u201cFile Decompressed\u201d<\/p>\n<h4>5. Transform stream in Nodejs:<\/h4>\n<p>In order to transform a stream, we use the transform method. For this we have to first initialize the transform module.<\/p>\n<p><strong>Code for Transforming stream:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const { Transform } = require('stream')\r\nconst TransformStream = new Transform();\r\nTransformStream._transform = (chunk, encoding, callback) =&gt; {\r\n    this.push(chunk.toString().toUpperCase());\r\n    callback();\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>The above code will internally convert the data into uppercase letters.<\/p>\n<h4>6. Stream object mode:<\/h4>\n<p>Stream generally expects string or buffer types of data. But if we want to use the javascript objects then we have to use the flag \u201cobjectmode\u201d.<\/p>\n<p><strong>Code for objectmode in stream:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const { Transform } = require('stream');\r\nconst commaSplitter = new Transform({\r\n    readableObjectMode: true,\r\n \r\n    transform(chunk, encoding, callback) {\r\n        this.push(chunk.toString().trim().split(','));\r\n        callback();\r\n    }\r\n});\r\nconst arrayToObject = new Transform({\r\n    readableObjectMode: true,\r\n    writableObjectMode: true,\r\n    transform(chunk, encoding, callback) {\r\n        const obj = {};\r\n        for (let i = 0; i &lt; chunk.length; i += 2) {\r\n            obj[chunk[i]] = chunk[i + 1];\r\n        }\r\n        this.push(obj);\r\n        callback();\r\n    }\r\n});\r\nprocess.stdin\r\n    .pipe(commaSplitter)\r\n    .pipe(arrayToObject)\r\n    .pipe(process.stdout)\r\n<\/pre>\n<h3>Nodejs Stream events:<\/h3>\n<p>Important events for readable stream:<\/p>\n<ul>\n<li><strong>Data Event:<\/strong> This event emits whenever a chunk of data is passed to a customer.<\/li>\n<li><strong>End event:<\/strong> This event emitted whenever there is no more data in the stream.<\/li>\n<\/ul>\n<p>Important events for writable stream:<\/p>\n<ul>\n<li><strong>Drain event:<\/strong> It implies that the writable stream can receive more data.<\/li>\n<li><strong>Finish event:<\/strong> It implies that all the data has been flushed.<\/li>\n<\/ul>\n<h3>Streams-powered Node.js APIs:<\/h3>\n<ul>\n<li><strong>Process.stdin:<\/strong> it returns a stream connected to stdin<\/li>\n<li><strong>Process.stdout:<\/strong> it returns a stream connected to stdout<\/li>\n<li><strong>Process.stderr:<\/strong> it returns a stream connected to stderr<\/li>\n<li><strong>http.request():<\/strong> returns a http clientrequest instance<\/li>\n<li><strong>zlib.createGzip():<\/strong> compress data into stream<\/li>\n<li><strong>zlib.createGunzip():<\/strong> decompress a gzip stream.<\/li>\n<li><strong>zlib.createInflate():<\/strong> decompress a deflate stream<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have discussed streams with codes and examples in detail, hope you liked reading it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be discussing streams, its advantages, its types, reading streams, writing streams, piping and also chaining. What is a stream in Nodejs? Streams are objects that we use to read&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":103988,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25669,25665,25670,25667,25668,25666],"class_list":["post-102271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-chaining-nodejs-stream","tag-changing-contents-of-nodejs-buffers","tag-nodejs-stream-events","tag-nodejs-streams","tag-piping-in-nodejs","tag-stream-in-nodejs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Streams in Nodejs - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about Streams in Nodejs. See its advantages, types, reading streams, writing streams, piping &amp; chaining. See stream objectmode &amp; events.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Streams in Nodejs - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about Streams in Nodejs. See its advantages, types, reading streams, writing streams, piping &amp; chaining. See stream objectmode &amp; events.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-05T03:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-06T05:50:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Streams in Nodejs - DataFlair","description":"Learn about Streams in Nodejs. See its advantages, types, reading streams, writing streams, piping & chaining. See stream objectmode & events.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/","og_locale":"en_US","og_type":"article","og_title":"Streams in Nodejs - DataFlair","og_description":"Learn about Streams in Nodejs. See its advantages, types, reading streams, writing streams, piping & chaining. See stream objectmode & events.","og_url":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-11-05T03:30:55+00:00","article_modified_time":"2021-11-06T05:50:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Streams in Nodejs","datePublished":"2021-11-05T03:30:55+00:00","dateModified":"2021-11-06T05:50:36+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/"},"wordCount":636,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp","keywords":["chaining nodejs stream","Changing contents of Nodejs buffers","Nodejs Stream events","Nodejs Streams","Piping in Nodejs","Stream in Nodejs"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/","url":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/","name":"Streams in Nodejs - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp","datePublished":"2021-11-05T03:30:55+00:00","dateModified":"2021-11-06T05:50:36+00:00","description":"Learn about Streams in Nodejs. See its advantages, types, reading streams, writing streams, piping & chaining. See stream objectmode & events.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-streams.webp","width":1200,"height":628,"caption":"nodejs streams"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/streams-in-nodejs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Streams in Nodejs"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=102271"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102271\/revisions"}],"predecessor-version":[{"id":103782,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102271\/revisions\/103782"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/103988"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=102271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=102271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=102271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}