Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

nodejs
stream
unpipe

What is Stream Module unpipe() in Node.js?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The readable.unpipe() method is applied to an already piped stream. unpipe() is a readable stream, which is used to detach a previously attached piped stream.

In Node JS, any streams can be piped using the stream.pipe() method.

Syntax

readable.unpipe(destination)

Parameter and return Value

Parameter

This method only takes one parameter. The destination parameter refers to the writable or piped stream, which we want to detach. Given the destination parameter is not defined, all existent pipes are detached.

Also, if a pipe is not set up for a given destination, the method returns nothing.

Return value

<stream.Writable> object is returned by this method.

In order for unpipe() to work, stream should be piped().

Code

// readable.unpipe() method
   
// Accessing fs module for reading writing files. 

const fs = require('fs');
  
// readable stream
const readable = fs.createReadStream("readable_file.text");
  
// writable Stream
const writable = fs.createWriteStream("writeable_file.text");
  
// Calling pipe method on the readable file to transfer contents of readable file to writable file.
readable.pipe(writable);
  
// Calling unpipe method to detach the piped writable file. 
readable.unpipe(writable);

Variables readable and writable contain the stream of readable and writable files.

RELATED TAGS

nodejs
stream
unpipe

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring