The Wayback Machine - https://web.archive.org/web/20241126232600/https://www.geeksforgeeks.org/node-js-stream-readable-pipe-method/
Open In App

Node.js Stream readable.pipe() Method

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The readable.pipe() method in a Readable Stream is used to attach a Writable stream to the readable stream so that it consequently switches into flowing mode and then pushes all the data that it has to the attached Writable. 

Syntax:

readable.pipe( destination, options )

Parameters: This method accepts two parameters as mentioned above and described below:

  • destination: This parameter holds the destination of writing data.
  • options: This parameter holds the pipe options.

Return Value: It returns the stream.Writable destination, allowing for a chain of pipes if it is a Duplex or a Transform stream.

peadable.pipe in node helps to enable the data transfer between streams by connecting the readable and writable streams.

Explore this Node.js Streams Module Complete Reference for detailed insights, practical examples, and expert guidance on handling data streams efficiently, from reading and writing to transforming and piping, to enhance your Node.js applications.

The below examples illustrate the use of readable.pipe() method in Node.js: 

Example 1: This example demonstrates the use of readable.pipe method to transfer data from input.text to output.text.

JavaScript
// Node.js program to demonstrate
// the readable.pipe() method

// Accessing fs module
const fs = require("fs");

// Create a readable stream
const readable = fs.createReadStream('input.txt');

// Create a writable stream
const writable = fs.createWriteStream('output.txt');

// Calling pipe method to transfer data
// from readable stream to writable stream
readable.pipe(writable);

console.log("Program Ended");

Output:

Program Ended

So, after the piping method the file named “output.text” must contain the data that was in the file “input.text”. 

Example 2: This example demonstrates using readable.pipe method to compress the input file data to input.text.gz

JavaScript
// Node.js program to demonstrate the 
// of streams using readable.pipe() method

// Accessing fs and zlib module
const fs = require("fs");
const zlib = require('zlib');

// Compress the file input.txt to 
// input.txt.gz using pipe() method
fs.createReadStream('input.txt')
   .pipe(zlib.createGzip())
   .pipe(fs.createWriteStream('input.txt.gz'));

console.log("File Compressed.");

Output:

File Compressed.

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


Previous Article
Next Article

Similar Reads

Node.js Stream readable.readable Property
The readable.readable property is an inbuilt application programming interface of Stream module which is used to check if it is safe to call readable.read() method. Syntax: readable.readable Return Value: It returns true if readable.read() method is being called otherwise returns false. Below examples illustrate the use of readable.readable propert
1 min read
Node.js Readable Stream readable Event
The 'readable' Event in a Readable Stream is emitted when the data is available so that it can be read from the stream or it can be emitted by adding a listener for the 'readable' event which will cause data to be read into an internal buffer. Syntax: Event: 'readable' Below examples illustrate the use of readable event in Node.js: Example 1: // No
1 min read
Node.js Stream readable.pause() Method
The readable.pause() method is an inbuilt application programming interface of Stream module which is used to stop the flowing mode from emitting 'data' events. If any data that becomes accessible will continue to exist in the internal buffer. Syntax: readable.pause() Parameters: This method does not accept any parameters. Return Value: If this met
2 min read
Node.js Stream readable.isPaused() Method
The readable.isPaused() method is an inbuilt application programming interface of Stream module which is used to check the current operating state of the Readable streams. Syntax: readable.isPaused() Parameters: This method does not accept any parameters. Return Value: It returns true if the Readable state is paused otherwise returns false. Below e
1 min read
Node.js Stream readable.destroy() Method
The readable.destroy() method is an inbuilt application programming interface of Stream module which is used to destroy the stream. Syntax: readable.destroy( error ) Parameters: This method accepts single parameter error which is optional and it emits an error while handling error event. Return Value: If this method is used then the stream is destr
2 min read
Node.js Stream readable.read() Method
The readable.read() method is an inbuilt application programming interface of Stream module which is used to read the data out of the internal buffer. It returns data as a buffer object if no encoding is being specified or if the stream is working in object mode. Syntax: readable.read( size ) Parameters: This method accepts single parameter size wh
2 min read
Node.js Stream readable.unpipe() Method
The readable.unpipe() method in a Readable Stream is used to detach a Writable stream which was previously attached while using the stream.pipe() method. Syntax: readable.unpipe( destination ) Parameters: This method accepts single parameter destination which is the destination of writable stream to be detached. Return Value: It returns stream.Writ
1 min read
Node.js Stream readable.setEncoding() Method
The readable.setEncoding() method in a Readable Stream is used to set the encoding of the data read. Syntax: readable.setEncoding( encoding ) Parameters: This method accepts single parameter encoding which holds the encoding type. Return Value: It returns the data in the encoded form. Below examples illustrate the use of readable.setEncoding() meth
1 min read
Node.js Stream readable.unshift() Method
The readable.unshift() method in a Readable Stream is utilized to push a chunk of data back into the internal buffer. However, when a stream is being fully consumed and it needs to be "un-consumed" then this method is helpful. Syntax: readable.unshift( chunk, encoding ) Parameters: This method accept two parameters as mentioned above and described
2 min read
Node.js Stream readable[Symbol.asyncIterator]() Method
The readable[Symbol.asyncIterator]() method in a Readable Stream is utilized to fully consume the stream. Syntax: readable[Symbol.asyncIterator]() Parameters: This method does not accept any parameters. Return Value: It returns asyncIterator to fully consume the stream. Below examples illustrate the use of readable[Symbol.asyncIterator]() method in
1 min read
Node.js stream.Readable.from() Method
The stream.Readable.from() method is an inbuilt application programming interface of the Stream module which is used to construct Readable Streams out of iterators. Syntax: stream.Readable.from( iterable, options ) Parameters: This method accept two parameters as mentioned above and described below: iterable: It is an object which implements the Sy
2 min read
Node.js Stream readable.resume() Method
Node.js streams are powerful tools for handling data transfer efficiently, especially when dealing with large datasets or real-time data. The readable.resume() method is a part of the readable stream interface, playing a crucial role in managing the flow of data. This article will delve into what readable.resume() does, how it works, and when to us
4 min read
Node.js Stream readable.destroyed Property
The readable.destroyed property is an inbuilt application programming interface of Stream module which is used to check the readable.destroy() function is being called or not. Syntax: readable.destroyed Return Value: It returns true if readable.destroy() method is being called otherwise returns false. Below examples illustrate the use of readable.d
2 min read
Node.js Stream readable.readableEncoding Property
The readable.readableEncodin property in a readable Stream is utilized to get the property encoding of a stated readable stream. Moreover, you can set this property using readable.setEncoding() method. Syntax: readable.readableEncoding Return Value: It returns the encoding used in the program. Below examples illustrate the use of readable.readableE
2 min read
Node.js Stream readable.readableEnded Property
The readable.readableEnded property in a readable Stream is utilized to check if the end event is emitted or not. Syntax: readable.readableEnded Return Value: It returns true if end event is emitted else it returns false. Below examples illustrate the use of readable.readableEnded property in Node.js: Example 1: // Node.js program to demonstrate th
1 min read
Node.js Stream readable.readableFlowing Property
The readable.readableFlowing property in a readable stream that utilized to check if the streams are in flowing mode or not. Syntax: readable.readableFlowing Return Value: It returns true if stream is in flowing mode else it returns false. Below examples illustrate the use of readable.readableFlowing property in Node.js: Example 1: // Node.js progr
1 min read
Node.js Stream readable.readableHighWaterMark Property
The readable.readableHighWaterMark property in a readable stream that is used to check the value of highWaterMark used while constructing Readable streams. Syntax: readable.readableHighWaterMark Return Value: It returns the value of highWaterMark used while constructing Readable streams. Below examples illustrate the use of readable.readableHighWat
1 min read
Node.js Stream readable.readableLength Property
The readable.readableLength property in a readable Stream that is used to check the number of bytes in the queue which is ready to be read. Syntax: readable.readableLength Return Values: It returns the number of bytes in the queue which are ready to be read. Below examples illustrate the use of readable.readableLength property in Node.js: Example 1
1 min read
Node.js Stream readable.readableObjectMode Property
The readable.readableObjectMode property in a Readable Stream that is used to check the objectMode of the stream. Syntax: readable.readableObjectMode Return Value: It returns the Boolean value. Below examples illustrate the use of readable.readableObjectMode property in Node.js: Example 1: // Node.js program to demonstrate the // readable.readableO
2 min read
Node.js Readable Stream error Event
The 'error' event in Readable stream can be emitted at any time. It takes place when the hidden stream is not able to generate data due to some hidden internal failure or when the implementation of stream pushes a chunk of data which is not valid. Moreover, a single Error object is passed as an argument to the listener callback. Syntax: Event: 'err
1 min read
Node.js Readable Stream end Event
The 'end' Event in a Readable Stream is emitted when there is no available data to be consumed from the readable stream. And the 'end' event won't be emitted if the data is not fully consumed. It can be done by switching the stream into the flowing mode, or by calling stream.read() method again and again until all the data is being consumed. Syntax
2 min read
Node.js Readable Stream data Event
The 'data' Event in a Readable Stream is emitted when readable.pipe() and readable.resume() method is called for switching the stream into the flowing mode or by adding a listener callback to the data event. This event can also be emitted by calling readable.read() method and returning the chunk of data available. Syntax: Event: 'data' Below exampl
2 min read
Node.js Readable Stream close Event
The 'close' Event in a Readable Stream is emitted when the stream and any of its hidden resources are being closed This event implies that no further events can be emitted, and no further computations can take place. Moreover, if a Readable stream is created with the emitClose option then it can always emit 'close' event. Syntax: Event: 'close ' Be
1 min read
Node.js Readable Stream resume Event
The 'resume' Event in a Readable Stream is emitted when stream.resume() is being called and readableFlowing property is not true. Syntax: Event: 'resume' Return Value: It is emitted if readable.resume() is being called else it is not emitted. Below examples illustrate the use of resume event in Node.js: Example 1: // Node.js program to demonstrate
2 min read
Node.js Readable Stream pause Event
The 'pause' Event in a Readable Stream is emitted when stream.pause() is being called and readableFlowing property is not false. Syntax: Event: 'pause' Return Value: It is emitted if readable.pause() is being called else it is not emitted. Below examples illustrate the use of pause event in Node.js: Example 1: // Node.js program to demonstrate the
1 min read
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. The below examples illustrate the use of the 'pipe' event
1 min read
Collect.js pipe() Method
The pipe() method is used to hold a callback function and apply this function into the collection and returns the corresponding result. Syntax: collect(array).pipe(callback)Parameters: The collect() method takes one argument that is converted into the collection and then the pipe() method is applied to it. The pipe() method holds the callback as a
2 min read
JQuery deferred.pipe() Method
The deferred.pipe() method in jQuery is used to add utility method to filter, chain Deferreds. Syntax: deferred.pipe([doneFilter][, failFilter][, progressFilter])Parameters: This method accepts three parameter as mentioned above and described below: doneFilter: It is an optional function which is called when the Deferred is resolved.failFilter: Thi
2 min read
Convert timestamp to readable date/time in PHP
Problem: Convert timestamp to readable date/time in PHP Solution: This can be achieved with the help of date() function, which is an inbuilt function in PHP can be used to format the timestamp given by time() function. This function returns a string formatted according to the given format string using the given integer timestamp or the current time
1 min read
JavaScript Convert bytes to human-readable string
Given the size of a file (in Bytes), the task is to convert it into human-readable form using JavaScript. Here are a few methods to discuss. Example 1: This example converts the file Size(in Bytes) into human-readable form. It shows the values in decimal and for less than 1024 Bytes, it remains in Bytes. C/C++ Code let size = function (bytes) { if
2 min read
three90RightbarBannerImg