Fun with JS streams
Published August 28th, 2025
In 2021, modern browsers and Node.js (v17+) adopted the WHATWG Streams standard, unlocking a powerful new way to handle data. Streams enable structured, incremental processing of data. Similar to Unix pipelines but built directly into the web platform and runtime. Streams come with a few key benefits:
-
Pipelines – Compose data transformations by chaining streams, just like command-line tools.
-
Backpressure-aware Queues – Automatically throttle producers and consumers to avoid overloading memory or CPU.
-
Async Iteration – Treat streams like async iterables, making integration with modern for await…of syntax seamless and intuitive.
Streams are usually thought of in the context of I/O. Here’s how an SSE client might be implemented with streams:
Another use-case is to turn events into streams. I like this pattern because:
-
You can enforce FIFO ordering ensuring each event is handled to completion before handling the next event.
-
For async await, syntax looks nicer to me.
-
It’s simple to compose multiple streams to add features like batching, deduplication, and logging in a re-usable and non-intrusive way.
-
Easy to implement queuing and buffering strategies without messing with your business logic.
Here’s a simple example of creating a flag update stream using the LaunchDarkly Node.js server SDK:
Usage:
This may seem somewhat trivial, and on the surface it is. But let’s take a look at how this can look with a full pipeline:
Streams give us nice lego blocks that we can use to compose different solutions. Here we can easily switch out our batching, logging and destination strategies.
This general pattern was used to create a proof of concept that subscribes to updates and evaluates flags for all of their site contexts and then publishes the results to Amazon SNS. Workers for their various applications will consume these queues and write the flags to their respective databases.
Now, a developer could leverage LaunchDarkly’s attribute based targeting and have the results be materialized in the database their application is already using for flags. There are obviously some caveats here, however, this system allows them to start using LaunchDarkly with no code changes and allows them to access flags from stored procedures critical to their business.
Streams make this implementation highly adaptable, letting us swap out different parts of the pipeline with ease. If you’re familiar with RxJS, these patterns may seem very familiar to you!
Anyway that’s all I have on streams. I like ‘em. Especially when combined with async iteration. Also, pro tip: you can turn any generator into a stream like this:
(ReadableStream.from(iteratable)
is an experimental API so your mileage may vary).