Node.js Streams Tutorial
In this tutorial, you will learn streams and working with streams in node.js
Introduction to Streams
- Streams are a way to handle data in Node.js in small, manageable chunks.
- They are especially useful for working with large files, network connections, and other data sources.
Types of Streams
- Readable: For reading data.
- Writable: For writing data.
- Duplex: For both reading and writing data.
- Transform: A special type of duplex stream for data transformation.
There are four main types of streams in Node.js
Creating Readable Streams
- 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
- 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
- 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
- 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
- Streams emit events like 'data', 'end', and 'error'.
- 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
- Data Processing
- Log Analysis
- Social Media Monitoring
- IoT
- Machine Learning