Node.js | Writable Stream pipe Event

The pipe event in a Writable Stream is emitted when the stream.pipe() method is being called on a readable stream by attaching this writable to its set of destinations.

Syntax:

Event: 'pipe'

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

Below examples illustrate the use of ‘pipe’ event in Node.js:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// pipe 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 pipe event
writable.on("pipe", readable => {
    console.log("Piped!");
});
  
// Calling pipe method
readable.pipe(writable);
  
console.log("Program Ended.");

chevron_right


Output:



Piped!
Program Ended.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// pipe 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 pipe event
writable.on("pipe", readable => {
    console.log("Piped!");
});
  
console.log("Program Ended");

chevron_right


Output:

Program Ended

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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.