X

Streams in NodeJS

Streams are essentially sequences of data that are read or written piece by piece (or chunk by chunk) rather than all at once.

In Node.js, streams are a powerful and efficient way to handle data flow. Streams are essentially sequences of data that are read or written piece by piece (or chunk by chunk) rather than all at once. This makes them particularly useful for working with large datasets or handling data in real-time.

There are four types of streams in Node.js:

1. Readable Streams:

Used for reading data from a source.
Events: data, end, error.

const fs = require('fs');
const readableStream = fs.createReadStream('example.txt');

readableStream.on('data', (chunk) => {
    console.log(chunk);
});

readableStream.on('end', () => {
    console.log('End of stream');
});

readableStream.on('error', (err) => {
    console.error(err);
});

2. Writable Streams:

Used for writing data to a destination.
Events: finish, error.

const fs = require('fs');
const writableStream = fs.createWriteStream('output.txt');

writableStream.write('Hello, ');
writableStream.write('world!');
writableStream.end();

writableStream.on('finish', () => {
    console.log('Write completed');
});

writableStream.on('error', (err) => {
    console.error(err);
});


3. Duplex Streams:

A stream that is both readable and writable.
Examples include TCP sockets.

const net = require('net');
const server = net.createServer((socket) => {
    // socket is both readable and writable
});


4. Transform Streams:

A special type of duplex stream where the output is computed based on the input.
Example: zlib compression stream.

const zlib = require('zlib');
const gzip = zlib.createGzip();
const readableStream = fs.createReadStream('example.txt');
const writableStream = fs.createWriteStream('example.txt.gz');

readableStream.pipe(gzip).pipe(writableStream);

In the example above, pipe() is used to pipe data from a readable stream to a writable stream. This is a common pattern in Node.js for handling streams.

Streams are efficient because they allow you to work with data in chunks, reducing memory usage and enabling better performance, especially when dealing with large files or network communication.