Web streams and streaming responses
The WHATWG ReadableStream in Node, converting to and from Node streams, streaming fetch bodies and HTTP responses, and the design problems of the API.
Web streams and streaming responses
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Node has two stream families: the classic `node:stream` classes, and the web-standard `ReadableStream`, `WritableStream` and `TransformStream`, which are globals and match what browsers, Deno, Bun and Workers provide.
- `fetch()` returns `response.body` as a web `ReadableStream`, and `new Response(stream)` streams a body out. Web streams are the native form for everything built on fetch.
- `Readable.fromWeb(webStream)` and `Readable.toWeb(nodeReadable)` convert between the two, and `Writable.toWeb` and `Writable.fromWeb` do the same for writables, so you can pass an upstream fetch body straight to an HTTP response.
- Web streams are async iterable in Node, so `for await (const chunk of response.body)` works. Breaking out of the loop cancels the stream.
- Web streams add per-read promises and locks. They are portable, but they cost more than Node streams in hot paths, which is the performance problem the video discusses.
Two stream APIs, one runtime
Node’s streams (node:stream) predate the web. When browsers later standardised their own streaming API for fetch, it ended up with different names and a different shape: ReadableStream with readers and locks, pipeThrough and pipeTo instead of pipe. Node implemented it, and as of current versions the classes are globals, with the same constructors also available from node:stream/web.
You will meet them mostly through fetch. response.body is a web ReadableStream, new Response(stream) takes one, and CompressionStream and TextDecoderStream are web TransformStreams. The standard also covers Deno, Bun and edge runtimes, so code written against it travels.
Using a web stream
const rs = ReadableStream.from(['a', 'b', 'c']);
const upper = new TransformStream({
transform(chunk, controller) { controller.enqueue(chunk.toUpperCase()); },
});
for await (const c of rs.pipeThrough(upper)) console.log(c); // A, B, C
Two things to know. Web streams are async iterable in Node, so the same for await loop from earlier lessons works, and breaking out of it cancels the stream. And they are locked when consumed: getReader() or pipeThrough() takes the lock, and a second consumer throws.
Crossing the boundary
Real programs have both kinds. Readable.fromWeb and Readable.toWeb convert in each direction:
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { createServer } from 'node:http';
const server = createServer(async (req, res) => {
const upstream = await fetch('https://example.com/export.csv');
res.writeHead(upstream.status, { 'content-type': 'text/csv' });
await pipeline(Readable.fromWeb(upstream.body), res);
});
The body is never held in memory in full. Backpressure travels from the slow client, through the Node stream, to the fetch read. Writable.toWeb(res) does the reverse when you want webStream.pipeTo(...). Chunk types do not always survive the trip. A web stream that enqueued strings came out of Readable.fromWeb as Buffers in my test (it is not in object mode by default), while Readable.toWeb(Readable.from(['p','q'])) kept the strings. Check what your stream carries before assuming.
Streaming responses and disconnects
Streaming a response is the useful part: send the first bytes now, the rest as they are ready. Server-sent events, large exports and proxying all work this way. The trap is the client that leaves. If your source keeps producing after the socket closes, you leak work. pipeline handles this: closing the socket errors the pipeline, destroys the chain, and calls the web stream’s cancel() so the source can stop. I ran a server that streamed ticks to a client that read three chunks and left, and cancel fired on the server as expected.
Why the web stream API is under fire
The Web Streams design makes every read a promise, wraps results in { value, done } objects, adds locks, and tee() buffers without limit for the slower branch. For one request in a framework, that can mean hundreds of short-lived stream objects. In Node, these costs show up in throughput. If your hot path moves many small chunks, prefer Node streams and convert at the edges, once.
What the video covers
This is a May 2026 Node Congress talk by James Snell, a Node.js TSC member who works on Cloudflare’s runtime. Its description lists: a hidden promise on every read and write, production failures (React SSR deadlocks, an AWS SDK problem, fetch holding connections), unbounded tee() buffering, and a proposed replacement built on plain iterators with backpressure that cannot be ignored, with 22 to 125 times speedups in benchmarks. It is a critique and a design proposal, not a how-to. The conversions, pipeline usage and disconnect handling above are this page’s. I could not read a transcript, so the proposal’s details are not summarised. It runs 39 minutes, so budget for that.
Where this fits
Web streams are the same backpressure idea as Node streams with different spelling. They matter in the next group: HTTP servers and fetch with undici both hand you streams. The browser side of fetch is in fetch and working with APIs.
Check yourself
You proxy an upstream API: `const up = await fetch(url)` and want to send its body to an Express-style `res` without buffering. How?
Convert the web stream and pipe it: `await pipeline(Readable.fromWeb(up.body), res)`. The body is never held in full, backpressure from the client reaches the upstream read, and a client disconnect destroys the chain.
When would you choose web streams over Node streams in a Node codebase?
When the code must run in several runtimes (Node, Deno, Bun, edge workers), or when it is built on `fetch` and `Response`, which use web streams natively. For Node-only work on files, sockets and HTTP servers, Node streams and `pipeline` are still the better-integrated and faster choice.
What does `readable.toWeb()` not give you for free?
A different performance profile. Every read through a web stream is an async operation, and bridging adds a layer of buffering and wrapping. In a tight loop moving many small chunks, the conversion can cost real throughput, so convert once at the boundary, not per chunk.
A client disconnects halfway through a streamed response. What should the server code do?
Stop producing. With `pipeline(Readable.fromWeb(body), res)` the pipeline errors and destroys both sides when the socket closes, which triggers the web stream's `cancel()` so the source can release its resources. In hand-written loops, listen for `res.on('close')` and abort the work.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.