Skip to content
Related Articles

Related Articles

Improve Article

Node.js Writable Stream unpipe Event

  • Last Updated : 12 Oct, 2021

The ‘unpipe’ event in a Writable Stream is emitted when stream.unpipe() method is being called on a Readable stream by detaching this Writable from its set of destinations.

Syntax:

 Event: 'unpipe'

Return Value: If the unpipe() method is being called then this event is emitted else its not emitted.

Below examples illustrate the use of ‘unpipe’ event in Node.js:

Example 1:






// Node.js program to demonstrate the     
// unpipe event
   
// Accessing fs module
var fs = require("fs");
  
// Create a readable stream
var readable = fs.createReadStream('input.txt');
  
// Create a writable stream
var writable = fs.createWriteStream('output.txt');
  
// Handling unpipe event
writable.on("unpipe", readable => {
    console.log("Unpiped!");
});
  
// Calling pipe method
readable.pipe(writable);
  
// Calling unpipe method
readable.unpipe(writable);
  
console.log("Program Ended...");

Output:

Unpiped!
Program Ended...

Example 2:




// Node.js program to demonstrate the     
// unpipe event
   
// Accessing fs module
var fs = require("fs");
  
// Create a readable stream
var readable = fs.createReadStream('input.txt');
  
// Create a writable stream
var writable = fs.createWriteStream('output.txt');
  
// Handling unpipe event
writable.on("unpipe", readable => {
    console.log("Unpiped!");
});
  
console.log("Program Ended...");

Output:

Program Ended...

So, here unpipe() function is not called so the unpipe event is not emitted.

Reference: https://nodejs.org/api/stream.html#stream_event_unpipe




My Personal Notes arrow_drop_up
Recommended Articles
Page :