What Is a Node.js Stream?
Tags: node, javascript
Published: 2021-08-05 13:18:39

---
title: What Is a Node.js Stream?
published: true
date: 2021-08-03 00:00:00 UTC
tags: node, javascript
canonical_url: https://www.mariokandut.com/what-is-a-node-js-stream/
---

Streams are a built-in feature in Node.js and represent asynchronous flow of data. Streams are also a way to handle reading and/or writing files. A Node.js stream can help process large files larger than the free memory of your computer, since it processes the data in small chunks.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#streams-in-nodejs)Streams in Node.js

This is the first article of a series about streams in Node.js. It aims to give an overview of different types of streams and what the limitations, benefits and use-cases are.

**Streams in Node.js**

- What is a Stream in Node.js? (this article)
- Connect streams with the pipe method (planned)
- Handle stream errors (planned)
- Connect streams with the pipeline method (planned)

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#what-are-streams)What are streams?

Streams are an interface for working with streaming data. Think of a Unix pipe `|` as a mental model of streams. Essentially, a stream is a collection of data, which isn't available at once. The streamed data arrives in small chunks. As a result we handle each data chunk when it arrives asynchronously.

In Node.js streams are used in many built-in modules to handle async data processing, for example, the `http` module uses streaming interfaces with `ClienRequest` and `ServerResponse`. Stream data is a buffer by default, unless it is configured to with objects. This means it helps to buffer the data in memory.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#why-use-streams)Why use streams?

**Streams let us work with data that is too large to fit into memory.** We can work with a chunk of data at a time. For instance, you are working with a 50gb file of analytics data with millions of rows. If you read this file into memory, it will take very long and eventually hit the memory limit of Node.js or of your local machine. Handling this file with a stream, we can process each row from the dataset at a time and don't have to read the file into memory. Hence, streams are **memory efficient**.

Streams are also useful in other scenarios. For example reading a large file into memory (assuming it fits), it would take some time to be readable. When consuming data from a stream, it's readable the moment a chunk of data arrives. This means streams are **time efficient** compared to reading data into memory.

Streams can be combined to and with other streams. For instance, the output of one stream can be used as the input for another stream. This allows us to combine streams into a pipeline through which data can flow between the streams. Hence, **streams are composable**.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#types-of-streams)Types of streams

There are 5 types of streams in the built-in `stream` module of Node.js. [docs](https://nodejs.org/api/stream.html#stream_stream)

- **Readable** : You receive data from a readable stream.
- **Writable** : You stream data to a writable stream. Also, referred as [sink](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream), because it is the end-destination of streaming data.
- **Duplex** : A duplex stream implements both interfaces - readable and writable. An example for duplex stream is a TCP socket, where data flows in both directions.
- **Transform** : Transform stream is a type of duplex stream, where the passing through data is transformed. So, the output will be different from the input. Data can be send to a transform stream, and read after it has been transformed.
- **PassThrough** : The PassThrough stream is a Transform stream, but doesn't transform data when passed through. It's mainly used for testing and examples.

Out in the wild there is a high possibility you will encounter `readable`, `writeable` and `transform` streams.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#stream-events)Stream Events

All streams are instances of `EventEmitter`. EventEmitters are used to emit and respond to events asynchronously. Read more about EventEmitters in the article [Event Emitters in Node.js](https://www.mariokandut.com/how-to-create-and-consume-events-node-js/). Events emitted by streams can be used to read and/or write data, manage the stream state, and handle errors.

Though streams are instances of `EventEmitter` it is not recommended, to handle streams like events and just listen to the events. Instead, the recommended way is to use the `pipe` and `pipeline` methods, which consume streams and handle the events for you.

Working with stream events is useful, when a more controlled way of how the stream is consumed is needed. For instance, triggering an event when a particular stream ends or begins. Have a look at the official Node.js docs regarding [Streams](https://nodejs.org/api/stream.html) for more information on this.

**Readable stream events**

- `data` - emitted when the stream outputs a data chunk.
- `readable` - emitted when there is data ready to be read from the stream.
- `end` - emitted when no more data is available.
- `error` - emitted when an error has occurred within the stream, and an error object is passed to the handler. **Unhandled stream errors can crash the application.**

**Writable stream events**

- `drain` will be emitted, when the writable stream's internal buffer has been cleared and is ready to have more data written into it.
- `finish` will be emitted, when all data has been written.
- `error` will be emitted when an error occurred while writing data, and an error object is passed to the handler. **Unhandled stream errors can crash the application.**

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Streams are an **interface for working with streaming data**.
- Stream data is a **buffer** by default.
- Streams are **memory efficient**. They consume only minimal amounts of memory.
- Streams are **time efficient** , data is readable as soon as the first chunk arrives.
- Streams are **composable** , they can be connected and combined with other streams.
- All streams are instances of EventEmitter, but listening to stream events is not the correct way of consuming a stream.
- Listening to stream events is useful, when you want to trigger something when the stream ends or starts.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

**References (and Big thanks):**

[HeyNode](https://heynode.com/tutorial/what-stream/),[Node.js - Streams](https://nodejs.org/api/stream.html#stream_stream),[MDN - Writable Stream](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream),[MDN - Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)