The Four Stream Types
You'll learn the four fundamental stream classes in Node.js and what each one does.
Now that you understand what streams are and why they're useful, it's time to meet the four types of streams that Node.js provides. Each type serves a specific purpose in data flow:
Readable Streams
Readable streams are sources you can consume data from. Think of them as a faucet pouring data out. Examples include reading from a file, receiving an HTTP request body, or getting data from standard input (process.stdin).
Writable Streams
Writable streams are destinations you can send data to. They're like a drain receiving water. Examples include writing to a file, sending an HTTP response, or outputting to the console (process.stdout).
Duplex Streams
Duplex streams are both readable and writable, but the two sides operate independently. Imagine a telephone line—you can speak and listen, but what you say doesn't affect what you hear. Network sockets are the classic example of duplex streams.
Transform Streams
Transform streams are a special type of duplex stream where the output is computed from the input. Think of a water filter: water goes in, filtered water comes out. Examples include compression (gzip), encryption, or data parsing. The readable side is directly connected to what you write to the writable side.
The Mental Model
- Readable: data flows out → you consume it
- Writable: data flows in → you write to it
- Duplex: independent two-way communication
- Transform: data flows through and gets modified
Key Takeaway: Node.js streams come in four types—Readable (source), Writable (destination), Duplex (independent two-way), and Transform (modifying passthrough)—each designed for different data flow patterns.