🚀 Streaming in Node.js: A Complete Guide with Examples

When working with large files (videos, images, logs, CSVs) or real-time data, loading everything into memory is inefficient. That’s where streams in Node.js come in.

Instead of reading/writing the whole file at once, streams break the data into small …


This content originally appeared on DEV Community and was authored by Rohit Singh

When working with large files (videos, images, logs, CSVs) or real-time data, loading everything into memory is inefficient. That’s where streams in Node.js come in.

Instead of reading/writing the whole file at once, streams break the data into small chunks. This makes Node.js apps faster, memory-efficient, and scalable.

🔹 What Are Streams in Node.js?

Streams are built-in objects in Node.js that allow reading/writing data piece by piece.

There are four main types of streams:

Readable – stream you can read from (e.g., fs.createReadStream)

Writable – stream you can write to (e.g., fs.createWriteStream)

Duplex – both readable and writable (e.g., TCP sockets)

Transform – streams that can modify data (e.g., compressing files)

🔹 Why Use Streams?

âś… Handle large files without crashing the server
âś… Improve performance & scalability
âś… Enable real-time processing (chat apps, video streaming, logging)
âś… Lower memory usage

🔹 Example 1: Reading a File Using Streams

Instead of reading an entire file with fs.readFile(), use streams:

const fs = require("fs");

// Create a readable stream
const readableStream = fs.createReadStream("bigfile.txt", {
  encoding: "utf-8",
  highWaterMark: 64 * 1024 // 64 KB chunks
});

// Listen to data events
readableStream.on("data", (chunk) => {
  console.log("Received chunk:", chunk.length);
});

readableStream.on("end", () => {
  console.log("File reading completed!");
});

👉 Here, the file is read in 64KB chunks, not all at once.

🔹 Example 2: File Copy Using Pipe

Node.js makes it super simple to pipe streams:

const fs = require("fs");

const readable = fs.createReadStream("input.txt");
const writable = fs.createWriteStream("output.txt");

// Pipe data from read → write
readable.pipe(writable);

console.log("File copied successfully using streams!");

âś… Efficiently copies large files without loading them fully into memory.

🔹 Example 3: Streaming an HTTP Response

Streams are great for serving large files in HTTP servers:

const http = require("http");
const fs = require("fs");

http.createServer((req, res) => {
  const stream = fs.createReadStream("video.mp4");
  res.writeHead(200, { "Content-Type": "video/mp4" });
  stream.pipe(res);
}).listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});

👉 Instead of loading the full video into memory, the server sends chunks to the client as they’re read.

🔹 Example 4: Transform Streams (Compression)

You can also transform data on the fly:

const fs = require("fs");
const zlib = require("zlib");

const readable = fs.createReadStream("input.txt");
const compressed = fs.createWriteStream("input.txt.gz");

// Compress file using gzip
readable.pipe(zlib.createGzip()).pipe(compressed);

console.log("File compressed successfully!");

âś… Useful for logs, backups, and optimizing storage.

🔹 Real-World Use Cases of Node.js Streams

Video/Audio streaming (Netflix, YouTube-like apps)

Large file uploads/downloads

Log processing in real-time

Chat applications with WebSockets

Data pipelines (ETL jobs)

🎯 Conclusion

Streams are a core strength of Node.js that let you handle large data efficiently without crashing servers. Whether you’re copying files, serving videos, or compressing data, streams make your app scalable, memory-friendly, and performant.

If you’re building high-performance apps in 2025, mastering Node.js streams is a must! 🚀

🚀 Rohit Singh 🚀 – Medium

Read writing from 🚀 Rohit Singh 🚀 on Medium. Full-stack developer with 6+ years in Angular, Node.js & AWS. Sharing tips, best practices & real-world lessons from building scalable apps.

favicon medium.com


This content originally appeared on DEV Community and was authored by Rohit Singh


Print Share Comment Cite Upload Translate Updates
APA

Rohit Singh | Sciencx (2025-09-02T14:36:50+00:00) 🚀 Streaming in Node.js: A Complete Guide with Examples. Retrieved from https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/

MLA
" » 🚀 Streaming in Node.js: A Complete Guide with Examples." Rohit Singh | Sciencx - Tuesday September 2, 2025, https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/
HARVARD
Rohit Singh | Sciencx Tuesday September 2, 2025 » 🚀 Streaming in Node.js: A Complete Guide with Examples., viewed ,<https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/>
VANCOUVER
Rohit Singh | Sciencx - » 🚀 Streaming in Node.js: A Complete Guide with Examples. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/
CHICAGO
" » 🚀 Streaming in Node.js: A Complete Guide with Examples." Rohit Singh | Sciencx - Accessed . https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/
IEEE
" » 🚀 Streaming in Node.js: A Complete Guide with Examples." Rohit Singh | Sciencx [Online]. Available: https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/. [Accessed: ]
rf:citation
» 🚀 Streaming in Node.js: A Complete Guide with Examples | Rohit Singh | Sciencx | https://www.scien.cx/2025/09/02/%f0%9f%9a%80-streaming-in-node-js-a-complete-guide-with-examples/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.