Suggest an editImprove this articleRefine the answer for “What is a Readable stream?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)A **Readable stream** is a stream type for reading data from a source in pieces, rather than all at once; it lets you work with large files or network data without loading the entire content into memory. **Key point:** it can run in flowing mode (data flows on its own through the `data` event) or paused mode (you call `.read()` yourself) - `.pipe()` uses the former and manages backpressure for you.Shown above the full answer for quick recall.Answer (EN)ImageA **Readable Stream** in Node.js is one of the stream types, meant **for reading data from a source in pieces**, rather than all at once. It lets you work with large files or data streams (for example, HTTP requests, sockets, file streams) **without loading the entire content into memory**. ### The core idea Instead of getting all the data at once, a Readable Stream delivers it **in chunks**. That makes processing: - **more memory-efficient**, especially with large files; - **asynchronous**, data is read as it arrives. ### Examples of Readable Streams in Node.js Several built-in objects already implement the Readable Stream interface: - `fs.createReadStream()`, reading files; - `http.IncomingMessage`, the body of an incoming HTTP request or response; - `process.stdin`, standard input; - `net.Socket`, TCP sockets. ### Readable Stream modes A Readable Stream can run in **two modes**: 1. **Flowing mode** Data is read automatically and immediately delivered to `data` event handlers. ```javascript const fs = require('fs'); const stream = fs.createReadStream('file.txt', 'utf8'); stream.on('data', chunk => { console.log('Got a chunk of data:', chunk); }); stream.on('end', () => { console.log('Reading finished'); }); ``` 2. **Paused mode** The stream waits for you to call `.read()` manually. ```javascript const fs = require('fs'); const stream = fs.createReadStream('file.txt', 'utf8'); stream.on('readable', () => { let chunk; while ((chunk = stream.read()) !== null) { console.log('Read a chunk:', chunk); } }); ``` ### Important Readable Stream events | Event | Description | |---|---| | `data` | Fires when a new chunk of data arrives | | `end` | The stream has finished, no more data | | `error` | An error occurred while reading | | `close` | The stream is fully closed | | `readable` | `read()` can be called, data is ready | ### Pipe (redirecting a stream) One of the most powerful uses is passing data directly into a Writable Stream: ```javascript const fs = require('fs'); const readable = fs.createReadStream('input.txt'); const writable = fs.createWriteStream('output.txt'); readable.pipe(writable); ``` > `pipe()` automatically manages the flow, it reads exactly as much as the other stream can keep up writing. ### Advantages of Readable Streams - Asynchronous (doesn't block the event loop) - Low memory use - Chainable (`pipe()`) - Supports backpressure (controlling transfer speed)For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.