Introduction to Readable Stream
Node.js Readable Streams are a crucial tool for efficiently handling large amounts of data. In this guide, we will explore the Readable Stream APIs through various code examples and discuss how to integrate them into a simple app.
What is a Readable Stream?
A Readable Stream is an abstraction that represents a source of data that you can read from. There are three main types of streams in Node.js: Readable, Writable, and Duplex (both Readable and Writable).
Creating a Simple Readable Stream
const { Readable } = require('stream'); const readableStream = new Readable({ read(size) { for (let i = 0; i < size; i++) { const chunk = generateRandomData(); this.push(chunk); } this.push(null); } }); function generateRandomData() { return Math.random().toString(36).substring(2); } readableStream.on('data', (chunk) => { console.log('Received chunk:', chunk); }); readableStream.on('end', () => { console.log('No more data.'); });
Using the pipe Method
One of the most powerful features of streams is the pipe
method, which allows you to forward data from a readable stream to a writable stream or a transform stream.
const { Writable } = require('stream'); const writableStream = new Writable({ write(chunk, encoding, callback) { console.log('Writing chunk:', chunk.toString()); callback(); } }); readableStream.pipe(writableStream);
Pausable Streams
Streams can be paused and resumed using the pause
and resume
methods respectively.
readableStream.pause(); console.log('Stream paused.'); setTimeout(() => { readableStream.resume(); console.log('Stream resumed.'); }, 1000);
Handling Errors in Streams
Error handling in streams is straightforward. Just listen for the error
event on the stream.
readableStream.on('error', (error) => { console.error('Error occurred:', error); });
Full Application Example
Let’s put together a simple application using all the introduced APIs.
const { Readable, Writable } = require('stream'); // Create a Readable Stream const readableStream = new Readable({ read(size) { for (let i = 0; i < size; i++) { const chunk = generateRandomData(); this.push(chunk); } this.push(null); } }); function generateRandomData() { return Math.random().toString(36).substring(2); } // Create a Writable Stream const writableStream = new Writable({ write(chunk, encoding, callback) { console.log('Writing chunk:', chunk.toString()); callback(); } }); // Handle errors readableStream.on('error', (error) => { console.error('Error occurred:', error); }); // Pipe the readable stream to the writable stream readableStream.pipe(writableStream); // Pause and resume the stream readableStream.pause(); console.log('Stream paused.'); setTimeout(() => { readableStream.resume(); console.log('Stream resumed.'); }, 1000);
In this application, we created a Readable Stream that generates random data chunks, and a Writable Stream that logs the data chunks to the console. We demonstrated pausing and resuming the stream, as well as handling errors.
Streams are a powerful feature in Node.js that can help you handle large amounts of data efficiently. By understanding and using streams, you can build scalable and high-performance applications.
Hash: 88369504f3f6e71ea4c90744ee9194f6b241ba5f469cb3b517f83bc460bded34