A Beginner’s Guide to Buffers, Streams, and Pipes in Node.js

Ketan Jakhar
2 min readMar 29, 2023

--

A Beginner’s Guide to Buffers, Streams, and Pipes in Node.js

If you’re new to Node.js, you might have come across terms like buffers, streams, and pipes. These concepts can be a bit confusing at first, but once you understand them, you’ll be able to take full advantage of Node.js’s capabilities.

Buffers

In Node.js, a buffer is a temporary storage area for binary data. Think of it as a container that holds a fixed amount of bytes. You can create a buffer using the Buffer class, like this:

const buffer = Buffer.from('hello');

In this example, we created a buffer that contains the string ‘hello’. But why would we need a buffer? Well, let’s say you want to read a file that’s too large to fit into memory all at once. You can use a buffer to read the file in chunks, like this:

const fs = require('fs');
const buffer = Buffer.alloc(1024);
const file = fs.openSync('myFile.txt', 'r');

let bytesRead = 0;
do {
bytesRead = fs.readSync(file, buffer, 0, buffer.length, null);
console.log(buffer.toString('utf-8', 0, bytesRead));
} while (bytesRead > 0);

fs.closeSync(file);

In this example, we’re using the fs module to open a file and read it in chunks of 1024 bytes. We're using the readSync() method to read the file into the buffer, and the toString() method to convert the binary data to a string.

Streams

A stream is a sequence of data that are processed in chunks. Think of it as a pipeline that carries data from one place to another. In Node.js, there are four types of streams: readable, writable, duplex, and transform. Each type is used for a different purpose.

Here’s an example of how to create a readable stream:

const fs = require('fs');
const readable = fs.createReadStream('myFile.txt');

In this example, we’re using the fs module to create a readable stream from a file. Once you have a readable stream, you can use the data event to read the data in chunks:

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

In this example, we’re logging each chunk of data as it’s read from the stream.

Pipes

A pipe is a mechanism for connecting two streams together. It allows you to take the output from one stream and send it directly to another stream, without having to manually process the data.

Here’s an example of how to use pipes:

const fs = require('fs');
const readable = fs.createReadStream('myFile.txt');
const writable = fs.createWriteStream('myOutputFile.txt');

readable.pipe(writable);

In this example, we’re creating a readable stream from a file and a writable stream to another file. We’re then using the pipe() method to connect the two streams together. As data is read from the readable stream, it's automatically written to the writable stream.

Hopefully, this beginner’s guide to buffers, streams, and pipes in Node.js has helped demystify these concepts. By using them, you can write more efficient and scalable Node.js applications.

--

--

Ketan Jakhar
Ketan Jakhar

Written by Ketan Jakhar

everything backend | NodeJS | TypeScript | Blockchain

No responses yet