Port Authority

Port patterns / Off the main thread: Actors, Remote DOM and media

Two workers that keep talking while this page is frozen

A port transferred into a Web Worker belongs to that worker's thread. Introduce two workers directly and their messages never touch the main thread again. Freeze this page for three seconds: its counter stops and every button locks up, while both workers keep exchanging messages and painting live into their own transferred OffscreenCanvas surfaces.

When you'd use this

A financial dashboard renders a live chart alongside a massive sortable table. Wiring the WebSocket parser worker directly to the OffscreenCanvas chart worker keeps the chart ticking smoothly even when sorting 50,000 rows stalls the main thread.

Code

const { port1, port2 } = new MessageChannel();
workerA.postMessage({}, [port1]);
workerB.postMessage({}, [port2]);
// this page is now out of the loop, and can stall without hurting them
while (performance.now() < until) {}

In practice

// live market feed: workerA parses WebSocket frames, workerB paints the chart
const workerA = new Worker("/parse.js"), workerB = new Worker("/draw.js");
const surface = chart.transferControlToOffscreen();
workerB.postMessage({ surface }, [surface]);

// /parse.js: send parsed ticks directly into workerB's port
const socket = new WebSocket("wss://prices.example/live");
onmessage = ({ ports: [toDraw] }) => { socket.onmessage = (e) => toDraw.postMessage(parse(e.data)); };

// /draw.js: paint ticks directly onto the OffscreenCanvas
let ctx = null;
onmessage = ({ data, ports: [fromParse] }) => {
  if (data.surface) ctx = data.surface.getContext("2d");
  if (fromParse) fromParse.onmessage = (e) => draw(ctx, e.data);
};

How it works

Every Web Worker runs on its own OS thread and event loop. Once you transfer the two ends of a MessageChannel into two workers, their traffic never touches the main thread again.

Blocking the main thread in a 3-second busy loop freezes the page's tick counter and buttons completely, while both workers keep exchanging messages and painting directly into their transferred OffscreenCanvas surfaces.

Step by step

What each press of Next does in the demo above.

  1. Introduce the two workers. Transfers port1 to Worker A and port2 to Worker B, snapping into a direct worker-to-worker channel.

What you can build with it

Built and running below.

A decoder worker feeding a renderer worker, while the page is busy

A decoder worker feeding a renderer worker directly. Keeping compute or sync pipelines responsive during main-thread layout or GC stalls.

One worker makes video frames. Another paints them into a canvas it was handed. This page introduces the two with a channel and steps away, so frames go from one worker straight to the other and never touch the page's thread. Freeze the page for three seconds, as a long layout or a garbage collection would, and the picture keeps moving.

Code

const { port1, port2 } = new MessageChannel();
decoder.postMessage({ out: port1 }, [port1]);
renderer.postMessage({ in: port2, surface }, [port2, surface]);

// the decoder: each frame's memory is moved to the renderer
out.postMessage({ pixels }, [pixels.buffer]);

Step by step

What each press of Next does in this demo.

  1. Connect the decoder to the renderer. Each worker gets one end of a channel. Frames start to flow between them.
  2. Freeze this page for 3 seconds. The page's counter stops. The picture does not.

Already running elsewhere

Things to try

  • Introduce the two workers (watch port1 and port2 fly from the page to Worker A and Worker B), then freeze the page for 3 seconds and try clicking any button while the workers keep flying.

Gotchas

  • Workers spawned by a page still terminate if the page closes or the OS discards the tab.
  • Fast producer-to-consumer worker ports have no built-in back-pressure — throttle or transfer a ReadableStream so queues don't grow unbounded.
  • Worker-to-worker channels are invisible to main-thread DevTools unless you wire telemetry or a wiretap.

Measured

Chromium 157: during a 3-second main-thread freeze, the page ticked 0 times while the workers kept exchanging messages every 100 ms. Other browsers are being filled in from the lab's results.

References

The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.

Web Workers
Scripts that run on their own thread, away from the page. MDNHTML Standard
OffscreenCanvas
A canvas that is not attached to the page, so a worker can draw to it. MDNHTML Standard
WebSocket
A two-way connection between a page and a server that stays open. MDNWebSockets Standard
MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
ReadableStream
A source of data that is read a piece at a time. It can be moved to another page or worker. MDNStreams Standard

For more, see Further reading: articles, talks, papers, libraries and the specifications, collected in one place.