Course contentsShow
← JavaScript
Lesson 1561 of 2,72334. Streams and BuffersFree lesson

What Are Duplex Streams?

Understanding streams that are both readable and writable, allowing bidirectional data flow.

What Are Duplex Streams?

You'll learn what duplex streams are and how they enable two-way data flow in Node.js applications.

The Core Concept

A duplex stream is a stream that is both readable and writable at the same time. Think of it like a telephone conversation: you can both speak (write) and listen (read) simultaneously. Unlike regular readable or writable streams that handle one-way data flow, duplex streams manage bidirectional communication.

The most common real-world example is a TCP socket connection. When your Node.js app connects to a server, you can write data to send requests and read data to receive responses through the same stream object.

How It Works

Duplex streams implement both the readable stream interface and the writable stream interface. This means they support all the events and methods you've already learned:

  • Readable side: 'data', 'end', 'readable' events, plus read() and pipe() methods
  • Writable side: write(), end() methods, plus 'drain' and 'finish' events

The two sides operate independently. Data written to the writable side doesn't automatically appear on the readable side—they're separate channels sharing the same stream object.

When You'll Use Them

You'll encounter duplex streams when working with:

  • Network sockets (TCP/HTTP connections)
  • Cryptographic operations
  • Compression/decompression
  • Any scenario requiring simultaneous read/write operations

Key Takeaway: Duplex streams combine readable and writable capabilities into a single stream object, enabling bidirectional data flow where reading and writing operate independently on separate channels.