Node.js Stream readable.resume() Method Last Updated : 05 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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 use it. Table of Content What is a Readable Stream What is readable.resume( )How Does readable.resume( ) WorkWhat is a Readable Stream In Node.js, a readable stream is an abstraction for a source of data that you can read from. Examples include reading from a file, receiving HTTP requests, or processing data from other data sources. Readable streams are instances of the Readable class, which is part of the stream module. Readable streams can operate in two modes: Paused Mode: The stream does not emit data events and you must explicitly call methods like read() to get data.Flowing Mode: The stream emits data events as soon as data is available, and it is automatically consumed.What is readable.resume( )The readable.resume() method is used to switch a readable stream into flowing mode. When a stream is in flowing mode, data is read from the underlying source and provided to your program without requiring explicit calls to read(). How Does readable.resume( ) Work ?When you call readable.resume(), the stream starts emitting data events, delivering chunks of data to the provided listeners. This method is particularly useful when you want to process data as it becomes available, rather than waiting for the entire dataset to be loaded. Syntax: readable.resume()In the below example: A readable stream is created from a file named example.txt.The data event listener logs each chunk of data received.The end event listener logs a message when the stream ends.The resume() method is called to switch the stream to flowing mode and start processing data immediately.const fs = require('fs');// Create a readable stream from a fileconst readableStream = fs.createReadStream('example.txt');// Handle the data eventreadableStream.on('data', (chunk) => { console.log(`Received ${chunk.length} bytes of data.`); console.log(chunk.toString());});// Handle the end eventreadableStream.on('end', () => { console.log('No more data.');});// Resume the stream to start flowing datareadableStream.resume();Example 1: Below examples illustrate the use of readable.resume() method in Node.js. javascript // Node.js program to demonstrate the // readable.resume() method // Including fs module const fs = require('fs'); // Constructing readable stream const readable = fs.createReadStream("input.text"); readable.on('data', (chunk) => { console.log(`${chunk}`); }); // Calling pause method readable.pause(); // Calling resume method readable.resume(); console.log("Data starts flowing again!!"); Output: Data starts flowing again!!Hello!!!Example 2: Below examples illustrate the use of readable.resume() method in Node.js. App.js // Node.js program to demonstrate the // readable.resume() method // Include fs module const fs = require('fs'); // Create readable stream const readable = fs.createReadStream("input.text"); // Handling data event readable.on('data', (chunk) => { console.log(`${chunk}`); // Calling pause method readable.pause(); // After this any data will be displayed // after 3 sec. console.log('No additional data will be ' + 'displayed for 3 seconds.'); // Using setTimeout function setTimeout(() => { console.log('Now data starts flowing again.'); // Calling resume method readable.resume(); }, 3000); }); // Displays that program // is ended console.log("Program ends!!"); Output: Program ends!!Hello!!!No additional data will be displayed for 3 seconds.Now data starts flowing again.When to Use readable.resume()Processing Data in Real-Time: Use resume() when you need to handle data as soon as it arrives, such as streaming media or real-time analytics.Avoiding Memory Overhead: By processing data in chunks, you avoid loading the entire dataset into memory, which is beneficial for handling large files.Compatibility with Event Listeners: If you have set up data event listeners and want to ensure they start receiving data, resume() will trigger the flow.Important ConsiderationsError Handling: Always attach an error event listener to handle potential errors in the stream.Backpressure: While resume() switches the stream to flowing mode, be aware of backpressure mechanisms that help manage the rate of data flow, preventing overwhelming the application.Pausing a Stream: If you need to temporarily stop the flow of data, you can call readable.pause().ConclusionThe readable.resume() method is a crucial tool in the Node.js streaming API, enabling efficient, real-time data processing. By understanding how to switch between paused and flowing modes, you can better manage data flow and build performant, scalable applications. Remember to handle errors gracefully and consider backpressure when dealing with high-throughput data streams. Comment More infoAdvertise with us Next Article Node.js Stream writable.writableLength Property N nidhi1352singh Follow Improve Article Tags : Web Technologies Node.js Node.js-Stream-module Similar Reads Writable Streams MethodsNode.js Stream writable.cork() MethodThe writable.cork() method is an inbuilt application programming interface of Stream module which is used to write every data into the buffer memory. When we use stream.uncork() or stream.end() methods then the buffer data will be flushed. Syntax: writable.cork() Parameters: This method does not ac 2 min read Node.js Stream writable.destroy() MethodThe writable.destroy() method is an inbuilt application programming interface of Stream module which is used to destroy the created stream and you cannot call the write() method to write data again after you have already destroyed the created stream. Syntax:writable.destroy()Parameters: This method 2 min read Node.js Stream writable.end() MethodThe writable.end() method is an inbuilt application programming interface of Stream module so that no more data can be written to the Writable anymore. The arguments chunk and encoding are optional which will permit one final new chunk of data to be written instantly before closing the stream. Moreo 3 min read Node.js Stream writable.setDefaultEncoding() MethodThe writable.setDefaultEncoding() method is an inbuilt application programming interface of Stream module which is used to set the default encoding for a Writable stream. Syntax: writable.setDefaultEncoding( encoding ) Parameters: This method accepts single parameter encoding which holds the encodin 2 min read Node.js Stream writable.uncork() MethodThe writable.uncork() method is an inbuilt application programming interface of Stream module which is used to flush all the buffered data when stream.cork() method was called. Syntax: writable.uncork() Parameters: This method does not accept any parameters. Return Value: If this method is being cal 2 min read Node.js Stream writable._write() MethodThe writable._write() method is an inbuilt application programming interface of Stream module which is used to implement a writable stream. The writable._write() method is affixed with an underscore as it is inside the class that defines it. Moreover, the user program must not call it directly. This 3 min read Node.js Stream writable.write() MethodThe writable.write() method is an inbuilt application programming interface of Stream module which is used to write some data to the Writable stream. The callback function is called once the data has been completely handled. Syntax: writable.write( chunk, encoding, callback) Parameters: This method 2 min read Node.js Stream readable.read() MethodThe 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 ) Parame 2 min read Node.js Stream readable.destroy() MethodThe 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 Valu 2 min read Node.js Stream readable.pause() MethodThe 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 no 2 min read Node.js Stream readable.isPaused() MethodThe 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 Readabl 1 min read Node.js Stream readable.resume() MethodNode.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 re 4 min read Writable Streams PropertyNode.js Stream writable.writableLength PropertyThe writable.writableLength property is an inbuilt application of stream module which is used to check the number of bytes in the queue that is ready to be written. Syntax: writable.writableLength Return Value: This property returns the number of bytes that is ready to write into the queue. Below 2 min read Node.js Stream writable.writableObjectMode PropertyThe writable.writableObjectMode property in Stream module is used to get the object mode value of the Writable stream. Syntax: writable.writableObjectMode Return Value: It returns true if the objectMode is set to true otherwise returns false. Below examples illustrate the use of writable.writableObj 2 min read Node.js Stream writable.writableFinished PropertyThe writable.writableFinished property is set to true instantly before the emit of the 'finish' event. Syntax: writable.writableFinished Return Value: It returns true if 'finish' event is called before it else it returns false. Below examples illustrate the use of writable.writableFinished property 2 min read Node.js Stream writable.writableCorked PropertyThe writable.writableCorked property is an inbuilt application programming interface of Stream module which is used to check the number of times you need to call the uncork() function so that you can fully uncork the stream. Syntax: writable.writableCorked Return Value: It returns an integer value t 2 min read Node.js Stream writable.destroyed PropertyThe writable.destroyed property is an inbuilt application programming interface of Stream module which is used to check the writable.destroy() method is being called or not. Syntax: writable.destroyed Return Value: This property returns true if writable.destroy() method is called before it else it r 2 min read Node.js Stream writable.writable PropertyThe writable.writable property is an inbuilt application programming interface of Stream module which is used to check the writable.write() method is safe to call or not. Syntax: writable.writable Return Value: It returns true if it is safe to call writable.write() method otherwise returns false. Be 2 min read Node.js Stream writable.writableEnded PropertyThe writable.writableEnded property is an inbuilt application programming interface of Stream module which is used to check the writable.end() method is being called or not. Syntax: writable.writableEnded Return Value: It returns true if writable.end() method is being called before otherwise returns 3 min read Node.js Stream writable.writableHighWaterMark PropertyThe writable.writableHighWaterMark property is an inbuilt application programming interface of stream module which is used to check the highWaterMark value which was passed while creating the Writable. Syntax: writable.writableHighWaterMark Return Value: It returns the value of highwatermark if it i 2 min read Node.js Stream readable.destroyed PropertyThe 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 fals 2 min read Writable Streams EventNode.js stream.Writable close EventThe stream.Writable close Event is an inbuilt application programming interface of Stream module which is used to emit when the stream and any of its hidden resources (for example, a file descriptor) is being closed. This event implies that no further events will be emitted, plus no further computat 2 min read Node.js Writable Stream finish EventThe 'finish' event in a Writable Stream is emitted after the Calling of writable.end() method when all the data is being flushed to the hidden system. Syntax: Event: 'finish' Return Value: If the writable.end() method is being called before then this event is emitted else its not emitted. Below exam 2 min read Node.js Writable Stream pipe EventThe 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 b 1 min read Node.js Writable Stream unpipe EventThe 'unpipe' event in a Writable Stream is emitted when the 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 it's not emit 1 min read Readable Streams MethodsNode.js Stream readable.pipe() MethodThe 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 2 min read Node.js Stream readable.unpipe() MethodThe 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 1 min read Node.js Stream readable.unshift() MethodThe 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 accep 2 min read Node.js stream.Readable.from() MethodThe 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: 2 min read Node.js Stream readable.setEncoding() MethodThe 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 1 min read Readable Streams PropertyNode.js Stream readable.readableLength PropertyThe 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 read 1 min read Node.js Stream readable.readableHighWaterMark PropertyThe 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 examp 1 min read Node.js Stream readable.readableFlowing PropertyThe 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.readableFlowin 1 min read Node.js Stream readable.readableEnded PropertyThe 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.j 2 min read Node.js Stream readable.readableObjectMode PropertyThe 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: javascrip 2 min read Node.js Stream readable.readable PropertyThe 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 exampl 1 min read Readable Streams EventsNode.js Readable Stream pause EventThe '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: 1 min read Node.js Readable Stream resume EventThe '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 2 min read Node.js Readable Stream error EventThe '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 argu 1 min read Node.js Readable Stream readable EventThe '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 1 min read Node.js Readable Stream data EventThe '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 2 min read Node.js Readable Stream close EventThe '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 alw 1 min read Node.js Readable Stream end EventThe '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 2 min read Transform Streams MethodsNode.js Stream transform.destroy() MethodThe transform.destroy() method in a Readable Stream is used to destroy the transform stream and also emits an 'error' event optionally. Moreover, the transform stream releases any internal resources after this call is made. Syntax: transform.destroy( error ) Parameters: This method accepts single p 2 min read Node.js Stream.pipeline() MethodThe stream.pipeline() method is a module method that is used to the pipe by linking the streams passing on errors and accurately cleaning up and providing a callback function when the pipeline is done. Syntax: stream.pipeline(...streams, callback) Parameters: This method accepts two parameters as m 2 min read Node.js stream.finished() MethodThe stream.finished() method is utilized to receive an alert if a stream is not writable or readable anymore or if it had experienced an error or a close event that is immature. Syntax:  stream.finished(stream, options, callback) Parameters: This method accepts three parameters as mentioned above a 3 min read Like