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

Lottie animation as a splash activity in Android 12+

Setting a Lottie animation as a splash activity in Android 12+ involves a few key steps, primarily leveraging the  SplashScreen  API introduced in Android 12 (API level 31). Here's a comprehensive guide: 1. Add Dependencies: Make sure your project uses Android Gradle Plugin 7.0.0 or higher and Gradle 7.0 or higher. Add the necessary dependencies to your module-level  build.gradle  file: dependencies { implementation 'androidx.core:core-splashscreen:1.0.1' // Or latest version implementation 'com.airbnb.android:lottie:latest_version' // Or latest version } 2. Create Your Lottie Animation: Design or obtain your Lottie animation file ( .json ). Place the animation file in your  res/raw  folder. If the  raw  folder doesn't exist, create it. 3. Implement the Splash Screen: In your  AndroidManifest.xml , set the theme of your splash activity to a theme that extends  Theme.SplashScreen : < resources > < style name = "The...

How edit host file in windows 10 and windows 11

  Editing the hosts file in Windows 10 and Windows 11 can be done using a text editor like Notepad. However, since the hosts file is a system file, you will need to run Notepad as an administrator to make changes. Here are the steps on how to do it: Windows 10: Open Notepad. Right-click on the Notepad icon in the taskbar and select "Run as administrator. " In the Notepad window, click on the "File" menu and select "Open. " Navigate to the following location: C: \Windows\System32\drivers\etc Select the "hosts" file and click on the "Open" button. Make the desired changes to the hosts file. Click on the "File" menu and select "Save. " Windows 11: Open Notepad. Click on the file menu and select "Open with". Select "Notepad (Administrator)" from the list of apps. In the Notepad window, click on the "File" menu and select "Open. " Navigate to the following lo...

Top 10 Mobiles with Dolby Atmos in 2023

Dolby Atmos is a surround sound technology that creates a more immersive and realistic audio experience. It does this by using multiple speakers to create a three-dimensional soundstage. This means that you can hear sounds coming from all around you, including above and below you. Benefits of Dolby Atmos in Mobile: More immersive audio experience: Dolby Atmos can create a more immersive audio experience for movies, music, and games. This can make you feel like you are right in the middle of the action. Clearer and more detailed sound: Dolby Atmos can also make the sound clearer and more detailed. This can be especially beneficial for music, as it can help you to hear all of the nuances of the instrumentation and vocals. Wider soundstage: Dolby Atmos can create a wider soundstage, which means that you can hear sounds coming from all around you. This can make you feel like you are surrounded by sound. More realistic sound effects: Dol...