Suggest an editImprove this articleRefine the answer for “Stream types”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Four types: **Readable** (read-only, like files, `req`), **Writable** (write-only, files, `res`), **Duplex** (both read and write, TCP sockets), and **Transform** (a Duplex that also transforms data on the fly, like gzip). **Key point:** all four are EventEmitters, and they're usually chained together with `.pipe()`, where each next stream gets the previous one's output.Shown above the full answer for quick recall.Answer (EN)Image## 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 | Type | Purpose | Example use | Main methods/events | |---|---|---|---| | **Readable** | Reading data | Reading files, HTTP requests, stdin | `.on('data')`, `.pipe()`, `.read()` | | **Writable** | Writing data | Writing a file, an HTTP response, stdout | `.write()`, `.end()`, `.on('finish')` | | **Duplex** | A two-way stream (read + write) | TCP sockets, WebSocket | `.write()`, `.read()`, `.pipe()` | | **Transform** | Duplex + 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 type | Key events | Main methods | |---|---|---| | **Readable** | `'data'`, `'end'`, `'error'` | `.read()`, `.pipe()`, `.pause()`, `.resume()` | | **Writable** | `'drain'`, `'finish'`, `'error'` | `.write()`, `.end()` | | **Duplex** | combines both sets | `.read()`, `.write()` | | **Transform** | `'data'`, `'finish'`, `'error'` | `.pipe()`, `.transform()` | ## 5. Where these stream types are used | Module | Uses streams for | |---|---| | `fs` | reading/writing files (`createReadStream`, `createWriteStream`) | | `http` | `req` and `res`, both streams (Readable/Writable) | | `zlib` | compression/decompression (Transform) | | `crypto` | encryption/hashing (Transform) | | `net` | TCP connections (Duplex) | | `process` | `stdin`, `stdout`, `stderr`, all streams | ## Summary | Type | Purpose | Example | |---|---|---| | **Readable** | A data source (reading) | `fs.createReadStream()`, `req` | | **Writable** | A data destination (writing) | `fs.createWriteStream()`, `res` | | **Duplex** | Reading + writing at once | `net.Socket`, WebSocket | | **Transform** | Reading + writing + transforming | `zlib.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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.