Node.js Course
Node.js
/
Advanced

Readable Streams

Definition

An abstraction for a source from which data is consumed (read) sequentially. Instead of reading an entire data source into memory at once, it reads it in small chunks.

Explain Like I'm New

Imagine drinking a massive 2-liter bottle of soda. A normal `fs.readFile` forces you to chug the entire 2-liter bottle in one single gulp (crashing your computer's RAM if it's too big). A Readable Stream gives you a straw. You sip a little bit, swallow, and sip a little more until the bottle is empty.

Real World Example

Reading a 50GB database backup file to search for a specific user ID.

Common Use Cases

  • •Reading massive files
  • •Parsing large HTTP request bodies
  • •Streaming video

Interactive Example

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

Interview Questions

basic

  • What event is emitted when a new chunk of data is ready?

intermediate

  • What event is emitted when the entire file is finished reading?

Flash Cards

Question

What event for a new chunk?

Click to reveal answer
Answer

The `'data'` event. e.g., `stream.on('data', (chunk) => { ... })`.

Question

What event when finished?

Click to reveal answer
Answer

The `'end'` event.