Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Class

Duplex

stream.Duplex is a bidirectional stream that implements both the Readable and Writable interfaces. Extend it or construct it with callbacks to define how data is read and written.

How it works

A Duplex is a stream with independent readable and writable sides. Create one with new Duplex(options), provide the readable implementation through read or _read(), and provide writable behavior through write or _write(); the two directions can be consumed and supplied separately.

Syntax

new stream.Duplex(options)

Parameters

NameTypeDescription
optionsObjectConfiguration object passed to the readable and writable stream constructors; it can include options such as allowHalfOpen and the read/write callbacks.

Returns

A new stream.Duplex instance.

Key points

  • A Duplex provides both input and output stream behavior in one object.
  • Common examples include TCP sockets, zlib streams, and crypto streams.
  • For a custom reading-and-writing stream, the documented implementation points include _read(), _write(), _writev(), and _final().
  • Node uses the Duplex class as the base for custom objects that need both stream directions.
  • A network connection is a representative use of a stream that both reads and writes.

Common mistakes

  • A subclass must implement the low-level operations needed for each direction, or provide the corresponding constructor callbacks.

Example: A minimal Duplex stream

import { Duplex } from 'node:stream';

const stream = new Duplex({
  read() {},
  write(chunk, encoding, callback) {
    process.stdout.write(`received: ${chunk.toString()}`);
    callback();
  }
});

stream.on('data', chunk => console.log(`read: ${chunk}`));
stream.push('from readable side');
stream.push(null);
stream.end('from writable side');

This runnable example creates a Duplex with separate readable and writable behavior, pushes one readable chunk, then writes one writable chunk.

When to use it

Bidirectional socket-style communication

Network connections

Streams that process data in both directions

Compression and encryption

Bridge a readable source and writable destination

Custom adapters

Interview questions

QWhat is a Duplex stream?

A Duplex implements both the Readable and Writable interfaces, so it can receive data and produce data independently. A TCP socket is a typical example.

Follow-up: How do you implement one? Extend Duplex and implement _read() and _write(), or pass read and write callbacks to its constructor.

QHow is a Duplex created?

Use new Duplex(options). The options configure both stream sides, while read and write callbacks provide the operations that the stream invokes for reading and writing.

Follow-up: What should a subclass do before using inherited buffering? Call the Duplex constructor so its buffering configuration is initialized.

QWhat does allowHalfOpen control?

The readable and writable halves are independent by default because allowHalfOpen defaults to true. With allowHalfOpen: false, ending one side automatically ends the other side.

Follow-up: When would the default be useful? Protocols that intentionally keep one direction open can use half-open behavior; use false for coupled lifetimes.

  • stream.Readable
  • stream.Writable
  • stream.Transform
  • stream.PassThrough
  • stream.pipeline()

Source: Stream | Node.js v26.11.1 Documentation. Checked 2026-10-15.