Stream
Stability: 2 - Stable
node:stream module provides an API for implementing the stream interface.
There are many stream objects provided by Node.js. For instance, a request to an HTTP server and process.stdout are both stream instances.
Streams can be readable, writable, or both. All streams are instances of EventEmitter.
Types of Streams
There are four fundamental stream types within Node.js:- Writable: streams to which data can be written (for example,
fs.createWriteStream()) - Readable: streams from which data can be read (for example,
fs.createReadStream()) - Duplex: streams that are both Readable and Writable (for example,
net.Socket) - Transform: Duplex streams that can modify or transform the data as it is written and read (for example,
zlib.createDeflate())
Object Mode
All streams created by Node.js APIs operate exclusively on strings, Buffers, TypedArray and DataView objects. It is possible, however, for stream implementations to work with other types of JavaScript values (with the exception ofnull). Such streams are considered to operate in “object mode”.
Stream instances are switched into object mode using the objectMode option when the stream is created.
Buffering
Both Writable and Readable streams will store data in an internal buffer. The amount of data potentially buffered depends on thehighWaterMark option passed into the stream’s constructor. For normal streams, the highWaterMark option specifies a total number of bytes. For streams operating in object mode, the highWaterMark specifies a total number of objects.
API for Stream Consumers
Almost all Node.js applications, no matter how simple, use streams in some manner. The following is an example of using streams in a Node.js application that implements an HTTP server:Writable Streams
Writable streams are an abstraction for a destination to which data is written. Examples of Writable streams include:- HTTP requests, on the client
- HTTP responses, on the server
- fs write streams
- zlib streams
- crypto streams
- TCP sockets
- child process stdin
process.stdout,process.stderr
writable.write(chunk[, encoding][, callback])
Added in: v0.9.4
The data to write
The encoding, if chunk is a string
Callback for when this chunk of data is flushed
Returns false if the stream wishes for the calling code to wait for the ‘drain’ event to be emitted before continuing to write additional data; otherwise true.
writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled.
writable.end([chunk[, encoding]][, callback])
Added in: v0.9.4
Optional data to write
The encoding if chunk is a string
Callback for when the stream is finished
writable.end() method signals that no more data will be written to the Writable. The optional chunk and encoding arguments allow one final additional chunk of data to be written immediately before closing the stream.
Event: ‘drain’
If a call towritable.write(chunk) returns false, the ‘drain’ event will be emitted when it is appropriate to resume writing data to the stream.
Event: ‘finish’
The ‘finish’ event is emitted after thewritable.end() method has been called, and all data has been flushed to the underlying system.
Event: ‘error’
The error object
Readable Streams
Readable streams are an abstraction for a source from which data is consumed. Examples of Readable streams include:- HTTP responses, on the client
- HTTP requests, on the server
- fs read streams
- zlib streams
- crypto streams
- TCP sockets
- child process stdout and stderr
process.stdin
readable.read([size])
Added in: v0.9.4
Optional argument to specify how much data to read
Returns data from the internal buffer. If no data is available to be read, null is returned.
readable.read() method reads data out of the internal buffer and returns it. If no data is available to be read, null is returned. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method.
readable.pipe(destination[, options])
Added in: v0.9.4
The destination for writing data
readable.pipe() method attaches a Writable stream to the readable, causing it to switch automatically into flowing mode and push all of its data to the attached Writable. The flow of data will be automatically managed so that the destination Writable stream is not overwhelmed by a faster Readable stream.
readable.unpipe([destination])
Added in: v0.9.4
Optional specific stream to unpipe
readable.unpipe() method detaches a Writable stream previously attached using the readable.pipe() method.
Event: ‘data’
The chunk of data
Event: ‘end’
The ‘end’ event is emitted when there is no more data to be consumed from the stream.Event: ‘error’
The error object
readable.setEncoding(encoding)
Added in: v0.9.4
The encoding to use
readable.setEncoding() method sets the character encoding for data read from the Readable stream.
Duplex and Transform Streams
Class: stream.Duplex
Duplex streams are streams that implement both the Readable and Writable interfaces. Examples of Duplex streams include:- TCP sockets
- zlib streams
- crypto streams
Class: stream.Transform
Transform streams are Duplex streams where the output is in some way related to the input. Like all Duplex streams, Transform streams implement both the Readable and Writable interfaces. Examples of Transform streams include:- zlib streams
- crypto streams
stream.pipeline(…streams[, callback])
Added in: v10.0.0
Two or more streams to pipe between
Called when the pipeline is complete
stream.finished(stream[, options], callback)
Added in: v10.0.0
A readable and/or writable stream
A callback function that takes an optional error argument
Stream Promises API
Thestream/promises API provides an alternative set of asynchronous utility functions for streams that return Promise objects rather than using callbacks.