Node.js / Node.js Streams and Buffers

Piping and Chaining Streams in Node.js

This tutorial explores the concepts of piping and chaining streams in Node.js. You will learn how to efficiently manage data flow between streams.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores working with streams and buffers for handling large data efficiently.

Piping and Chaining Streams in Node.js

1. Introduction

In this tutorial, we are going to explore the concepts of piping and chaining streams in Node.js. By the end of this tutorial, you will master how to efficiently manage data flow between streams.

You will learn:

  • What are Streams in Node.js?
  • What is piping and why it is important?
  • How to pipe and chain streams?

Prerequisites:

  • Basic understanding of Node.js
  • Familiarity with JavaScript syntax and ES6+ features
  • Node.js and npm installed on your machine

2. Step-by-Step Guide

Streams are collections of data that might not be available all at once. They let you handle data in chunks as it comes in or goes out, which is crucial for handling large amounts of data or real-time data.

Piping is a mechanism where we provide the output of one stream as the input to another stream. It is normally used to get data from one stream and pass the output of that stream to another stream. There is no limit on piping operations.

Piping Streams

Here's an example of piping between a readable and writable stream:

const fs = require('fs');

let readableStream = fs.createReadStream('input.txt');
let writableStream = fs.createWriteStream('output.txt');

// pipe the read and write operations
// read input.txt and write data to output.txt
readableStream.pipe(writableStream);

console.log("Piping Finished");

In this example, we're reading data from input.txt and writing it to output.txt.

Chaining Streams

Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Let's look at an example:

const fs = require('fs');
const zlib = require('zlib');

// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
  .pipe(zlib.createGzip())
  .pipe(fs.createWriteStream('input.txt.gz'));

console.log('File Compressed');

In this code, we're chaining .pipe() calls. We're reading from input.txt, compressing it using gzip (provided by the zlib module), and then writing it to input.txt.gz.

3. Code Examples

Example 1: Piping Streams

This example copies the content from the source file to the destination file using piping.

const fs = require('fs');

// Create a readable stream
let readableStream = fs.createReadStream('source.txt');

// Create a writable stream
let writableStream = fs.createWriteStream('destination.txt');

// Pipe the two streams
readableStream.pipe(writableStream);

console.log("Piping Complete");

This script reads from source.txt and pipes the output into destination.txt.

Example 2: Chaining Streams

This example reads data from a file, compresses it, and writes it to a new file.

const fs = require('fs');
const zlib = require('zlib');

// Create a readable stream
let readableStream = fs.createReadStream('uncompressed.txt');

// Create a writable stream
let writableStream = fs.createWriteStream('compressed.txt.gz');

// Create gzip object
let gzip = zlib.createGzip();

// Pipe the streams
readableStream.pipe(gzip).pipe(writableStream);

console.log("Chaining Complete");

This script reads from uncompressed.txt, compresses the data, and writes the compressed data to compressed.txt.gz.

4. Summary

In this tutorial, we covered the concept of piping and chaining streams in Node.js. We learned how to use piping to direct data from a readable stream to a writable stream, and how to use chaining to connect multiple operations together.

For further learning, consider exploring other stream methods like stream.write(data[, encoding][, callback]), stream.end([data][, encoding][, callback]), and events like data, end, and error.

5. Practice Exercises

Exercise 1:

Create a script that copies content from one file to another. Use piping.

Solution:

const fs = require('fs');

let readStream = fs.createReadStream('file1.txt');
let writeStream = fs.createWriteStream('file2.txt');

readStream.pipe(writeStream);

console.log('Content copied!');

Exercise 2:

Create a script that reads a text file, converts the text to uppercase, and writes the result to a new file. Use piping and chaining.

Solution:

const fs = require('fs');
const through2 = require('through2');

let readStream = fs.createReadStream('lowercase.txt');
let writeStream = fs.createWriteStream('uppercase.txt');

readStream
  .pipe(through2(function(chunk, _, next) {
    this.push(chunk.toString().toUpperCase());
    next();
  }))
  .pipe(writeStream);

console.log('Text converted to uppercase!');

In this solution, we are using the through2 module to handle the transformation. This module provides a simpler API for dealing with streams.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help