A ReadableStream is transferable too, so you can hand another origin a live stream and it reads with back-pressure as if the source were local. Chrome, Firefox and Safari (27+) support transferable streams.
When you'd use this
A spreadsheet app hands a million-row sheet to a third-party chart iframe without serialising the whole dataset into RAM. It transfers a ReadableStream that slices 500 rows whenever the chart asks for more; if the chart falls behind, back-pressure automatically throttles the spreadsheet. Works in Chrome, Firefox, and Safari 27+.
Code
const stream = newReadableStream({ pull(controller) { controller.enqueue(nextChunk()); } });
port.postMessage({ stream }, [stream]);
// other side
port.onmessage = async ({ data }) => { forawait (const chunk of data.stream) use(chunk); };
In practice
// spreadsheet app streaming a huge table to a third-party chart iframelet next = 0;
const nextChunk = () => sheet.rows.slice(next, (next += 500));
const stream = newReadableStream({
pull(controller) { // only runs when the chart has room for moreif (next >= sheet.rows.length) return controller.close();
controller.enqueue(nextChunk());
},
});
try { port.postMessage({ stream }, [stream]); }
catch { showError("This browser can't transfer streams yet."); }
// in the chart iframeasyncfunctionuse(rows) { await chart.add(rows); }
port.onmessage = async ({ data }) => {
forawait (const chunk of data.stream) awaituse(chunk); // slow chart slows pull()
chart.finish();
};
How it works
A ReadableStream can go straight in the transfer list. Underneath, the browser wires an internal MessageChannel between the source and reader: chunks cross as they're produced, so the reader starts consuming long before the writer finishes.
What a stream buys you over raw postMessage is built-in back-pressure. The browser only pulls new chunks when the reader has room, so a slow consumer on another origin automatically throttles a fast producer on yours.
Transferring a stream locks and detaches it in the sender — ownership moves cleanly.
Step by step
What each press of Next does in the demo above.
Open a channel. Transfers port2 to the reader frame on another origin.
Send a stream. Transfers a live ReadableStream down the port. The frame pulls chunks with back-pressure while this page is still producing them.
What you can build with it
Built and running below.
An answer that appears in another origin as it is written
Streaming incremental output (like LLM tokens) to a viewer frame.
A language model answers a word at a time. This page stands in for one, and hands the answer to a viewer on another origin as a stream. The viewer shows each word as it arrives. It does not poll and is not sent a message per word by hand: it reads a stream, and when this page stops, the stream ends and the viewer knows the answer is complete.
Code
const answer = newReadableStream({ asyncpull(controller) {
const word = awaitnextWord();
word ? controller.enqueue(word) : controller.close();
} });
viewer.contentWindow.postMessage({ answer }, viewerOrigin, [answer]);
// the viewerforawait (const word of answer) page.append(word + " ");
Step by step
What each press of Next does in this demo.
Stream an answer to the viewer. The answer is handed over as a stream before it has been written. Words appear on the other origin as they are produced.
Stop writing. This page ends the stream early. The viewer is told it is over.
Stream 64 MB to a worker a megabyte at a time
Handing a large download or generated file to a worker or another origin without buffering the whole thing in RAM. Tracking progress chunk by chunk.
This page produces a large file a megabyte at a time and gives the worker a stream of it. The worker reads, adds up a checksum and asks for more. The page makes the next megabyte only when asked, so neither side ever holds more than a chunk or two. The progress bar needs no extra messages, because it counts the chunks.
Code
const file = newReadableStream({ pull(controller) {
made < TOTAL ? controller.enqueue(nextMegabyte()) : controller.close(); // only when asked
} });
worker.postMessage({ file }, [file]);
// the workerforawait (const chunk of file) { sum = checksum(sum, chunk); postMessage({ read: chunk.length }); }
Step by step
What each press of Next does in this demo.
Stream 64 MB to the worker. The worker is handed a stream. Each megabyte is made when the worker asks for it, and checked as it lands.
Things to try
Open a channel, send a stream, and watch the teal ReadableStream badge cross before chunks start arriving one by one.
Send a second stream right away — each stream is independent.
Gotchas
If you forward a stream through an intermediate context, chunks keep flowing through that middleman — if it closes, the reader stalls silently with no error (introduce with a MessagePort first, then send the stream directly).
Chunks are structured-cloned by default unless you transfer underlying buffers.
A write to a transferred WritableStream can resolve before the far side rejects it.
The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.
ReadableStream
A source of data that is read a piece at a time. It can be moved to another page or worker. MDNStreams Standard
<iframe>
An element that shows another page inside this one. The inner page can be on a different origin, and the two can only talk by posting messages. MDNHTML Standard
Transferable objects
Objects that can be moved to another page or worker instead of copied. After the move the sender can no longer use them. The transfer list is the argument to postMessage that names them. MDNHTML Standard
MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
MessagePort.postMessage()
Sends a message through a port. A second argument lists the objects to move instead of copy. MDNHTML Standard
MessagePort
One end of a message channel. Whatever is posted into it comes out of the other end, and it can be sent to another page, frame or worker. MDNHTML Standard
WritableStream
A destination that data is written to a piece at a time. It can be moved to another page or worker. MDNStreams Standard