Node.js Streams Tutorial

In this tutorial, you will learn streams and working with streams in node.js

Introduction to Streams

  1. Streams are a way to handle data in Node.js in small, manageable chunks.
  2. They are especially useful for working with large files, network connections, and other data sources.

Types of Streams

    There are four main types of streams in Node.js

  1. Readable: For reading data.
  2. Writable: For writing data.
  3. Duplex: For both reading and writing data.
  4. Transform: A special type of duplex stream for data transformation.

Creating Readable Streams

  1. To create a readable stream, you can use built-in classes like fs.createReadStream() for reading files or manually create a custom readable stream.
const fs = require('fs');

const readableStream = fs.createReadStream('example.txt');
readableStream.on('data', (chunk) => {
    console.log(chunk.toString());
});
    

Creating Writable Streams

  1. To create a writable stream, you can use built-in classes like fs.createWriteStream() for writing to files or manually create a custom writable stream.
const fs = require('fs');

const writableStream = fs.createWriteStream('output.txt');
writableStream.write('Hello, World!', 'utf-8');
    

Piping Streams

  1. Piping is a technique to connect a readable stream to a writable stream directly.
const fs = require('fs');

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

readableStream.pipe(writableStream);
    

Transform Streams

  1. Transform streams are a special type of duplex stream used for data transformation. You can use the transform stream to modify data as it passes through.
const { Transform } = require('stream');

const upperCaseTransform = new Transform({
    transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
    },
});

process.stdin.pipe(upperCaseTransform).pipe(process.stdout);
    

Handling Errors and Events

  1. Streams emit events like 'data', 'end', and 'error'.
  2. It's important to handle errors and manage resources, especially when working with streams.

    readableStream.on('data', (chunk) => {
        console.log(chunk.toString());
      });
      
      readableStream.on('end', () => {
        console.log('End of data.');
      });
      
      readableStream.on('error', (err) => {
        console.error('Error:', err);
      });
      

Applications of Streams in Real Time

  1. Data Processing
  2. Log Analysis
  3. Social Media Monitoring
  4. IoT
  5. Machine Learning