{"id":11399,"date":"2014-01-30T12:08:56","date_gmt":"2014-01-30T06:38:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11399"},"modified":"2014-02-03T12:09:32","modified_gmt":"2014-02-03T06:39:32","slug":"writable-streams-in-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/writable-streams-in-node-js\/","title":{"rendered":"Writable Streams In Node.Js"},"content":{"rendered":"<p style=\"text-align: justify\" dir=\"ltr\">In my previous blog we discussed about \u201c<a title=\"Streams in node.js : Readable and Writable\" href=\"http:\/\/www.tothenew.com\/blog\/streams-in-node-js-readable-writable\/\">Readable Streams in Node.js<\/a>\u201d. Here we will be learning about the next part of stream which is, writable stream.<\/p>\n<p style=\"text-align: justify\">Writable stream is used to write data to a specific destination.<\/p>\n<p style=\"text-align: justify\" dir=\"ltr\">Basically its has two main method :<\/p>\n<ol style=\"text-align: justify\">\n<li>\n<p dir=\"ltr\">write.<\/p>\n<\/li>\n<li>\n<p dir=\"ltr\">end<\/p>\n<\/li>\n<\/ol>\n<p style=\"text-align: justify\"><strong>Write:<\/strong><br \/>\n<code>writable.write(chunk,[encoding],[callback])<\/code><\/p>\n<p style=\"text-align: justify\" dir=\"ltr\">This method is used to write the chunk of data to stream and call its corresponding callback after writing the data, if it is provided. Encoding is supplied if the chunk is a String.This method will return true if data is handled or ready to write more data.<\/p>\n<p style=\"text-align: justify\" dir=\"ltr\"><strong>End:<\/strong><\/p>\n<p style=\"text-align: justify\"><code>writable.end(chunk,[encoding],[callback])<\/code><\/p>\n<p style=\"text-align: justify\" dir=\"ltr\">This method is used to write last chunk of data on the stream and call its corresponding callback when it is finished writing. Finish event is attached with this callback. Encoding is supplied if the chunk is a String.<\/p>\n<p style=\"text-align: justify\"><strong>Lets take an example:<\/strong><\/p>\n<p style=\"text-align: justify\" dir=\"ltr\">In this example I will be writing my first name using write() and the calling end() with my last name.<\/p>\n<p>[js]<br \/>\nhttp.createServer(req,res){<br \/>\nres.write(\u201cSahil \u201d);<br \/>\nres.end(\u201cChitkara\u201d)<br \/>\n}<br \/>\n[\/js]<\/p>\n<p style=\"text-align: justify\"><strong>Events:<\/strong><\/p>\n<ol style=\"text-align: justify\">\n<li>\n<p dir=\"ltr\"><strong>Drain<\/strong>: If writing data with writable.write(chunk) returns false, the drain event will indicate whether its is appropriate to write more data to stream.<\/p>\n<\/li>\n<li>\n<p dir=\"ltr\"><strong>Finish<\/strong>: This event is emitted when end() is called.<\/p>\n<\/li>\n<\/ol>\n<p style=\"text-align: justify\" dir=\"ltr\">Example<\/p>\n<p>[js]<br \/>\nhttp.createServer(req,res){<br \/>\nres.write(\u201cSahil \u201d);<br \/>\nres.end(\u201cChitkara\u201d);<br \/>\nres.on(\u2018finish\u2019\u2019,function(){<br \/>\nconsole.log(\u201cwriting data is now complete\u201d);<br \/>\n});<br \/>\n}<br \/>\n[\/js]<\/p>\n<ol style=\"text-align: justify\" start=\"3\">\n<li>\n<p dir=\"ltr\"><strong>Pipe<\/strong>: This event is emitted when pipe() is called on readable stream and adding writable stream to its destination.<\/p>\n<\/li>\n<\/ol>\n<p>[js]<br \/>\nvar fs= require(\u2018fs\u2019)<br \/>\nvar readable = fs.createReadStream(\u2018abc.txt\u2019);<br \/>\nvar writable=fs.createWriteStream(\u2018bcd.txt\u2019);<br \/>\nreadable.pipe(writable);<br \/>\n[\/js]<\/p>\n<ol style=\"text-align: justify\" start=\"4\">\n<li>\n<p dir=\"ltr\"><strong>Unpipe<\/strong>: This event is emitted when unpipe() is called on readable stream and adding writable stream to its destination.<\/p>\n<\/li>\n<\/ol>\n<p>[js]<br \/>\nvar fs= require(\u2018fs\u2019)<br \/>\nvar readable = fs.createReadStream(\u2018abc.txt\u2019);<br \/>\nvar writable=fs.createWriteStream(\u2018bcd.txt\u2019);<br \/>\nreadable.unpipe(writable);<br \/>\n[\/js]<\/p>\n<ol style=\"text-align: justify\" start=\"5\">\n<li>\n<p dir=\"ltr\"><strong>Error<\/strong>: Emitted when error occurs while writing and piping the data.<\/p>\n<\/li>\n<\/ol>\n<p>[js]<br \/>\nvar fs= require(\u2018fs\u2019)<br \/>\nvar readable = fs.createReadStream(\u2018abc.txt\u2019);<br \/>\nvar writable=fs.createWriteStream(\u2018bcd.txt\u2019);<br \/>\nreadable.pipe(writable);<br \/>\nwritable.on(\u2018error\u2019,function(err){<br \/>\nconsole.log(\u201cerror: \u201d+err);<br \/>\n});<br \/>\n[\/js]<\/p>\n<p style=\"text-align: justify\">General use case of writable streams in nodejs:<\/p>\n<ul style=\"text-align: justify\">\n<li>http request<\/li>\n<li>http response<\/li>\n<li>fs write streams<\/li>\n<li>tcp sockets<\/li>\n<li>child process<\/li>\n<\/ul>\n<p style=\"text-align: justify\" dir=\"ltr\">So, this was a brief \u00a0intro of \u00a0writable streams. Hope you&#8217;ve learned some good stuff here.<\/p>\n<p style=\"text-align: justify\" dir=\"ltr\">Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blog we discussed about \u201cReadable Streams in Node.js\u201d. Here we will be learning about the next part of stream which is, writable stream. Writable stream is used to write data to a specific destination. Basically its has two main method : write. end Write: writable.write(chunk,[encoding],[callback]) This method is used to write the [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1185],"tags":[55,1124,1298,1269,1299],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11399"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11399"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11399\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}