Skip to main content

Stream types

1. A general definition

A stream is an abstraction that lets you read or write data gradually, in pieces (chunks), rather than loading it all into memory at once.

Every stream is an object that implements the EventEmitter interface (it emits events like 'data', 'end', 'error', 'finish').

2. The main stream types in Node.js

TypePurposeExample useMain methods/events
ReadableReading dataReading files, HTTP requests, stdin.on('data'), .pipe(), .read()
WritableWriting dataWriting a file, an HTTP response, stdout.write(), .end(), .on('finish')
DuplexA two-way stream (read + write)TCP sockets, WebSocket.write(), .read(), .pipe()
TransformDuplex + transforming data "on the fly"Compression (zlib), encryption (crypto).pipe(), .transform()

Readable Stream

This is a data source, a stream you read from.

Examples:

  • reading a file via fs.createReadStream(),
  • an HTTP request's body (req in http.createServer()),
  • an input stream (process.stdin).

Example:

javascript
const fs = require('fs'); const readable = fs.createReadStream('file.txt', 'utf8'); readable.on('data', chunk => { console.log('Got a chunk:', chunk.length); }); readable.on('end', () => { console.log('Reading finished'); });

The stream reads data gradually and fires the 'data' event for each chunk.

Writable Stream

This is a data destination, a stream you write to.

Examples:

  • writing to a file via fs.createWriteStream(),
  • sending a response (res) in an HTTP server,
  • printing to the console (process.stdout).

Example:

javascript
const fs = require('fs'); const writable = fs.createWriteStream('output.txt'); writable.write('First line\n'); writable.write('Second line\n'); writable.end('File written'); writable.on('finish', () => { console.log('Writing finished'); });

The stream accepts data in pieces via .write(), and signals completion through the 'finish' event.

Duplex Stream

This is a stream that can read and write data at the same time. It combines what Readable and Writable can do.

Examples:

  • network connections (net.Socket),
  • WebSocket connections,
  • a TCP stream (one stream both reads and writes).

Example:

javascript
const { Duplex } = require('stream'); const duplex = new Duplex({ read(size) { this.push('data from duplex\n'); this.push(null); }, write(chunk, encoding, callback) { console.log('Written:', chunk.toString()); callback(); } }); duplex.on('data', chunk => console.log('Read:', chunk.toString())); duplex.write('Hello Duplex!');

Duplex is useful for two-way exchange (for example, sockets, tunnels, protocols).

Transform Stream

This is a special kind of Duplex stream that transforms data "on the fly" as it passes through.

Examples:

  • compression (zlib.createGzip()),
  • encryption (crypto.createCipher()),
  • transforming text (for example, to uppercase).

Example:

javascript
const { Transform } = require('stream'); const upperCase = new Transform({ transform(chunk, encoding, callback) { this.push(chunk.toString().toUpperCase()); callback(); } }); process.stdin.pipe(upperCase).pipe(process.stdout);

Anything you type into the console prints back in uppercase.

3. How streams interact

Streams are usually chained with .pipe():

javascript
const fs = require('fs'); const zlib = require('zlib'); fs.createReadStream('file.txt') .pipe(zlib.createGzip()) // a Transform stream (compression) .pipe(fs.createWriteStream('file.txt.gz')); // a Writable stream

Here:

  • createReadStream → the data source (Readable)
  • createGzip → the transformation (Transform)
  • createWriteStream → the destination (Writable)

Streams connect like a pipeline, passing data in pieces without blocking memory.

4. Stream events and methods

Stream typeKey eventsMain methods
Readable'data', 'end', 'error'.read(), .pipe(), .pause(), .resume()
Writable'drain', 'finish', 'error'.write(), .end()
Duplexcombines both sets.read(), .write()
Transform'data', 'finish', 'error'.pipe(), .transform()

5. Where these stream types are used

ModuleUses streams for
fsreading/writing files (createReadStream, createWriteStream)
httpreq and res, both streams (Readable/Writable)
zlibcompression/decompression (Transform)
cryptoencryption/hashing (Transform)
netTCP connections (Duplex)
processstdin, stdout, stderr, all streams

Summary

TypePurposeExample
ReadableA data source (reading)fs.createReadStream(), req
WritableA data destination (writing)fs.createWriteStream(), res
DuplexReading + writing at oncenet.Socket, WebSocket
TransformReading + writing + transformingzlib.createGzip(), crypto.createCipher()

Conclusion:

Node.js has four types of streams, Readable, Writable, Duplex, and Transform.

They let you:

  • read, write, and transform data gradually,
  • work with large files and network streams efficiently,
  • and build data-processing pipelines without loading all the data into memory.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.