Node.js Course
Node.js
/
Advanced

Duplex Streams

Definition

A stream that implements both Readable and Writable interfaces. It represents a two-way communication channel.

Explain Like I'm New

A Walkie-Talkie or a Telephone. You can speak into it (Writable) AND you can listen to it (Readable) at the exact same time. The data you write is entirely independent of the data you read.

Real World Example

A TCP Network Socket. When a user connects to your multiplayer game, you get a Duplex Stream. You `read` from it to see their joystick movements, and you `write` to it to send them the positions of the other players.

Common Use Cases

  • •Network sockets (TCP)
  • •WebSockets

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What is an example of a Duplex stream?

intermediate

  • Is the readable side connected to the writable side in a Duplex stream?

Flash Cards

Question

Example of a Duplex stream?

Click to reveal answer
Answer

A TCP socket connection (`net.Socket`).

Question

Are they connected?

Click to reveal answer
Answer

No. In a standard Duplex stream, the read and write channels are independent. If you want a stream where the output depends on the input (like compressing a file), you use a `Transform` stream instead.