0
0
Node.jsframework~5 mins

Reading data with Readable streams in Node.js

Choose your learning style9 modes available
Introduction

Readable streams let you get data piece by piece instead of all at once. This helps when working with big files or slow sources.

Reading a large file without loading it all into memory
Receiving data from a network request bit by bit
Processing data as it arrives from a sensor or device
Handling user input streams like keyboard or microphone
Reading data from a database query that returns a stream
Syntax
Node.js
const readable = getReadableStream();

readable.on('data', (chunk) => {
  // handle each chunk of data
});

readable.on('end', () => {
  // all data read
});

The 'data' event gives you chunks of data as they arrive.

The 'end' event tells you when no more data is coming.

Examples
Read a text file chunk by chunk and print each part.
Node.js
const fs = require('node:fs');
const readable = fs.createReadStream('file.txt');

readable.on('data', (chunk) => {
  console.log('Got chunk:', chunk.toString());
});

readable.on('end', () => {
  console.log('Finished reading file');
});
Create a readable stream from an array of strings and print them as they come.
Node.js
const { Readable } = require('node:stream');

const readable = Readable.from(['Hello', ' ', 'world', '!']);

readable.on('data', (chunk) => {
  process.stdout.write(chunk);
});

readable.on('end', () => {
  console.log('\nDone');
});
Sample Program

This program reads the file 'example.txt' in small parts. Each part is printed as it arrives. When done, it prints a message.

Node.js
import { createReadStream } from 'node:fs';

const readable = createReadStream('example.txt', { encoding: 'utf8' });

readable.on('data', (chunk) => {
  console.log(`Received chunk: ${chunk}`);
});

readable.on('end', () => {
  console.log('No more data.');
});
OutputSuccess
Important Notes

Always handle the 'error' event on streams to catch problems.

Readable streams can be paused and resumed if needed.

Use encoding option to get strings instead of buffers.

Summary

Readable streams let you read data bit by bit, saving memory.

Listen to 'data' for chunks and 'end' when done.

Use streams for large files, network data, or any slow source.