Skip to main content

 

Stream in NodeJS

In Node.js, streams are a powerful way to handle data in a continuous, chunk-by-chunk manner. Instead of loading an entire file or data source into memory, streams process data as it becomes available, making them highly efficient for large files, network requests, and other I/O operations.

There are four main types of streams:

  • Readable: A stream from which you can read data. Examples include fs.createReadStream() for files and the http.IncomingMessage object on a server.
  • Writable: A stream to which you can write data. Examples include fs.createWriteStream() for files and the http.ServerResponse object for a server's response.
  • Duplex: A stream that is both Readable and Writable. An example is a net.Socket.
  • Transform: A Duplex stream that can modify or transform data as it is written and read. A good example is the zlib module for compression and decompression.

How to Use Streams

The most common and recommended way to work with streams is using the pipe() method. Piping connects a readable stream to a writable stream, so data flows from one to the other automatically.

Example 1: Reading and Writing Files

This is a classic example of using streams to copy a large file without consuming excessive memory.

const fs = require('fs');

// Create a readable stream from the source file
const readableStream = fs.createReadStream('source.txt');

// Create a writable stream to the destination file
const writableStream = fs.createWriteStream('destination.txt');

// Pipe the readable stream to the writable stream
// This will read data from source.txt and write it to destination.txt
readableStream.pipe(writableStream);

// You can also listen for events to know when the operation is complete
writableStream.on('finish', () => {
console.log('File copy complete.');
});

Example 2: Using Events (Low-Level Approach)

While pipe() is the easiest method, you can also use events to handle streams. This gives you more fine-grained control over the data flow.

Readable Stream Events:

  • data: Fired when a chunk of data is ready to be read.
  • end: Fired when there is no more data to be read.
  • error: Fired if an error occurs.

Writable Stream Events:

  • finish: Fired when the end() method has been called, and all data has been written.
  • error: Fired if an error occurs.

Here’s how you would manually handle a readable stream and pipe it to a writable stream using events:

const fs = require('fs');

const readableStream = fs.createReadStream('source.txt');
const writableStream = fs.createWriteStream('destination.txt');

// Read data chunk by chunk
readableStream.on('data', (chunk) => {
// Write the chunk to the writable stream
writableStream.write(chunk);
});

// When the readable stream ends, close the writable stream
readableStream.on('end', () => {
writableStream.end();
});

// Handle errors
readableStream.on('error', (err) => {
console.error('Error reading from file:', err);
});
writableStream.on('error', (err) => {
console.error('Error writing to file:', err);
});

When to Use Streams

Use streams in Node.js whenever you are dealing with large amounts of data or when the data is not immediately available, such as:

  • Reading or writing large files.
  • Handling data from network requests (HTTP, TCP sockets).
  • Processing compressed data using zlib.
  • Working with data from other processes.

Comments

Popular posts from this blog

  How to 'CEO' Your Website: A Layperson's Guide to SEO Introduction: Why Your Website Needs a CEO (and what 'SEO' even means!) Ever wonder why some websites pop up first on Google while others are buried deep, lost in the digital ether? It's not some arcane magic ritual, though at times it might feel like it. It's SEO. Search Engine Optimization. But what  is  SEO, really? Strip away the jargon and it's basically this: your website's personal marketing manager, albeit one powered by algorithms and caffeine. This manager's job is to make sure search engines (like Google, still the king of the hill, though challengers are emerging) understand what your site is about and, crucially, show it to the  right  people. Think of it as translation, from human intent to machine understanding, and back again. The goal? Simple. More eyes on your amazing content, your groundbreaking products, your life-altering services. And all this without having to bleed yo...

Why cromecast need internet access?

  Chromecast devices require internet access for several reasons: Content Streaming: Chromecast devices primarily function as streaming devices, enabling users to cast content from various streaming services like Netflix, Hulu, Disney+, YouTube, and many more. This content is hosted on the internet, and Chromecast needs an internet connection to access and stream it to your TV. Device Setup and Updates: During the initial setup process, Chromecast devices need to connect to the internet to download and install the necessary software and updates. This ensures that your device is running the latest version of the Chromecast software, providing optimal performance and security. Content Casting: Even when casting content from your local device, such as a smartphone or tablet, Chromecast still requires an internet connection. This is because the casting process involves communicating with the Chromecast device over the internet to initiate the casting session, ...
Google Opal Lands in India: Your AI Sidekick for App Building (No Code Required!)   I. Introduction: Meet Your New App-Building Bestie! Ever dreamt of creating an app, but the thought of coding sends shivers down your spine? What if you could simply  tell  an AI what you want, much like whispering a secret wish to a digital genie? The very notion feels like something out of a futuristic fable. Well, pinch yourself, because the future might just be here. Google Opal is officially here in India, having touched down on October 7th or 8th, 2025, depending on how you slice the temporal pie. This isn't just another tech launch; it's Google Labs' experimental, no-code AI application builder. Think of it as a creative playground where your ideas are the only limit. Imagine morphing your mental sketches into functional mini-apps with just a few natural language prompts. It's alchemy of a sort, turning thought into (digital) gold. What Google is offering is nothing sh...