Streams and Buffers
Introduction to Streams and Buffers in Node.js
Before we dive into the world of streams and buffers in Node.js, let's first understand the basics.
What is a Buffer?
A Buffer is a temporary storage spot for a chunk of data that is being transferred from one place to another. It is especially useful when the data cannot be processed immediately. Buffers are widely used when there is a difference between the rate at which data is received and the rate at which it can be processed.
In Node.js, buffers are implemented within the Buffer
class, which is a global class that can be accessed in an application without importing the buffer
module.
Here is how you create a buffer in Node.js:
let buffer = Buffer.alloc(10);
// This will create a buffer of 10 bytes.
In Node.js, each character in a Buffer
is represented by a decimal from 0 to 255.
What is a Stream?
Streams are objects that allow you to read data from a source or write data to a destination in a continuous fashion. In Node.js, there are four types of streams:
- Readable: They are used for read operation.
- Writable: They are used for write operation.
- Duplex: They can be used for both read and write operation.
- Transform: A type of duplex stream where the output is computed based on the input.
Each type of Stream is an instance of EventEmitter
. They emit events at different times which can be listened to with functions.
Here is how you can create a readable stream:
let fs = require("fs");
let data = '';
// Create a readable stream
let readerStream = fs.createReadStream('input.txt');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
data += chunk;
});
readerStream.on('end',function(){
console.log(data);
});
readerStream.on('error', function(err){
console.error(err.stack);
});
console.log("Program Ended");
In the above example, we have created a readable stream from a file input.txt
. On receiving data, the function is appending data to a variable. At the end of the stream, data from the file is logged to the console.
The Relationship Between Buffers and Streams
Buffers and streams in Node.js are closely related. Streams either produce or consume data. Buffers, on the other hand, temporarily hold this data.
When a stream has produced data that is yet to be consumed, this data will need a temporary place to be stored. This is where buffers come in.
In Node.js, you can create a stream that operates on strings or buffers. The Buffer
class in Node.js makes it easy to work with binary data, which is what streams are under the hood.
In conclusion, buffers and streams are fundamental concepts in Node.js that you should understand. They provide a way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient manner.