Port Authority

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

Worker-to-Worker media pipeline with zero main-thread hops

Browser video and image editors like Photopea, Riverside and CapCut Web can't afford to bounce pixels through the main thread every frame. Hand a same-origin <canvas> straight to a Render Worker with transferControlToOffscreen(), then wire Source Worker → Filter Worker → Render Worker directly with two MessageChannels. Each ImageBitmap transfers zero-copy from worker to worker and never touches the main thread — even when you freeze this page for 800ms, the pipeline keeps painting.

When you'd use this

A browser-based video editor or live podcast studio decodes incoming frames, runs colour grading or background blur, and paints a live preview canvas while the user drags timeline tracks and opens menus. Wiring Source → Filter → Render directly over MessageChannels and handing the preview <canvas> to the Render Worker keeps playback locked to the display refresh rate even during heavy main-thread UI interactions.

Code

// main thread: hand canvas to Render Worker and wire src -> flt -> rnd
const offscreen = canvas.transferControlToOffscreen();
const srcToFlt = new MessageChannel(), fltToRnd = new MessageChannel();

srcWorker.postMessage({ cmd: "init" }, [srcToFlt.port1]);
fltWorker.postMessage({ cmd: "init" }, [srcToFlt.port2, fltToRnd.port1]);
rndWorker.postMessage({ cmd: "init", canvas: offscreen }, [offscreen, fltToRnd.port2]);

// Filter Worker: process frame, close input GPU bitmap, transfer output
inPort.onmessage = async ({ data: { frame, seq } }) => {
  ctx.drawImage(frame, 0, 0);
  frame.close();                                 // release GPU memory
  applyFilter(ctx);
  const out = await createImageBitmap(canvas);
  outPort.postMessage({ frame: out, seq }, [out]);
};

In practice

// video editor: chain decode -> shader filter -> compositor canvas off-thread
const preview = document.querySelector("#preview").transferControlToOffscreen();
const decodeToFx = new MessageChannel();
const fxToRender = new MessageChannel();

decoderWorker.postMessage({ cmd: "init" }, [decodeToFx.port1]);
fxWorker.postMessage({ cmd: "init" }, [decodeToFx.port2, fxToRender.port1]);
renderWorker.postMessage({ cmd: "init", canvas: preview }, [preview, fxToRender.port2]);

// UI sliders only send tiny parameter updates; pixels never touch this thread
lutSelect.onchange = () => fxWorker.postMessage({ cmd: "lut", name: lutSelect.value });
timelineScrub.oninput = () => decoderWorker.postMessage({ cmd: "seek", ms: +timelineScrub.value });

How it works

In a naive browser video or photo editor, frames bounce back and forth across the main thread: decode in Worker A, post to main thread, post to Filter Worker B, post back to main thread to draw on <canvas>. The moment the user scrolls, opens a menu or runs heavy UI JS, the main thread drops frames.

You can cut the main thread out of the pixel path completely. Call canvas.transferControlToOffscreen() once on your page's <canvas> and transfer the resulting OffscreenCanvas straight to a Render Worker. Then wire Source Worker → Filter Worker → Render Worker directly with two MessageChannels.

Each frame moves from Source to Filter and Filter to Render as a transferred ImageBitmap (or WebCodecs VideoFrame in full video pipelines). Transferring moves ownership in zero-copy memory between same-process workers, and because the Render Worker commits frames straight to the browser compositor, even an 800ms main-thread freeze drops zero frames.

Notice the critical discipline inside every stage: call frame.close() the instant you've drawn an incoming ImageBitmap. Bitmaps live in GPU/graphics memory that JavaScript's garbage collector doesn't track tightly — forget to close them and you'll chew through VRAM in seconds.

Step by step

What each press of Next does in the demo above.

  1. Wire workers and hand off OffscreenCanvas. This page hands its canvas drawing surface directly to the Render Worker via transferControlToOffscreen(), and connects Source to Filter and Filter to Render with direct MessageChannels.
  2. Start live Worker-to-Worker pipeline. Source generates 25fps frames, transfers each ImageBitmap straight to Filter (duotone), which transfers the filtered bitmap straight to Render. Zero frames touch the main thread.
  3. Switch filter to invert. A single tiny control message flips Filter Worker to invert mode. The pixel stream stays entirely off the main thread.
  4. Freeze main thread for 800ms. This page locks its own main thread in a real 800ms busy-wait. Watch the frame counter painted on the canvas keep climbing while the page is completely frozen.

What you can build with it

Other ideas

  • Browser-based video editors and recorders (like Riverside or CapCut Web) chaining WebCodecs decode, WebGL/WebGPU effects and encode without main-thread stalls.
  • Real-time camera pipelines applying background blur, colour grading or face-tracking overlays off the UI thread.
  • Live image editors and chart/GIS tile renderers (like Photopea) streaming compositor-ready surfaces while keeping complex toolbars responsive.

Things to try

  • Wire the pipeline and watch the OffscreenCanvas pill fly to the Render Worker while the two direct worker-to-worker channels snap into place.
  • Start the pipeline and switch the filter between invert and duotone: only a tiny mode string leaves the main thread while 25fps bitmaps stay strictly inside the workers.
  • Press Freeze main thread for 800ms and watch the frame counter painted on the canvas keep ticking right through the freeze.

Gotchas

  • Always call frame.close() (and close dropped frames when skipping backlog). Unclosed ImageBitmap and VideoFrame handles leak GPU memory fast and can crash the tab.
  • Keep transferControlToOffscreen() strictly to same-origin Dedicated Workers. Sending a transferred OffscreenCanvas to a cross-origin or sandboxed <iframe> crashes the frame's renderer process in Chrome.
  • If Source produces frames faster than Filter can process them, don't let messages pile up in the port queue — drop stale frames or use explicit back-pressure credits so latency doesn't balloon.
  • Once you call transferControlToOffscreen(), the main thread can never call getContext() or resize that canvas directly again; send resize messages to the Render Worker instead.

References

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

transferControlToOffscreen()
Hands the drawing of a canvas on the page to an OffscreenCanvas, which can then be sent to a worker. MDNHTML Standard
MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
ImageBitmap
A decoded image that is ready to draw. It can be moved to another thread without copying the pixels. MDNHTML Standard
OffscreenCanvas
A canvas that is not attached to the page, so a worker can draw to it. MDNHTML Standard
VideoFrame
One frame of video. It can be moved between threads without copying the pixels. MDNWebCodecs
Web Workers
Scripts that run on their own thread, away from the page. MDNHTML 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

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