Port Authority

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

Actor Model on the Web: treat the UI thread as just another actor

Most web apps hang workers off the main thread like helper subroutines, so every message between state, compute and storage bounces back through the UI thread. Paul Lewis and Surma flipped that model in their 2018 Chrome Dev Summit talk (and PolymerLabs/actor-helpers, later used in PROXX): treat the UI thread as just one peer actor. A tiny bootstrapper wires hookup() and lookup() by creating MessageChannels and transferring ends straight to the workers. After startup, State, Compute and Storage talk peer-to-peer with zero main-thread hops, and a stalled Compute worker won't drop a single UI frame.

When you'd use this

A local-first notes app has full-text search indexing, IndexedDB/OPFS syncing and a dense virtualised editor. Rather than bouncing search results and database writes through the main thread, the bootstrapper wires a State worker directly to the Search and Storage workers over MessageChannels. Typing stays snappy at 60fps even while Search re-indexes thousands of notes in the background.

Code

// bootstrapper: wire direct actor-to-actor MessageChannels, then step back
function link(actorA, nameB, actorB, nameA) {
  const { port1, port2 } = new MessageChannel();
  actorA.postMessage({ hookup: nameB }, [port1]);
  actorB.postMessage({ hookup: nameA }, [port2]);
}

link(uiActor, "state", stateWorker, "ui");
link(stateWorker, "compute", computeWorker, "state");
link(stateWorker, "storage", storageWorker, "state");

// inside State Actor: talk directly to Compute and Storage, 0 main-thread hops
uiPort.onmessage = ({ data }) => {
  if (data.type === "SOLVE") computePort.postMessage({ type: "COUNT_PRIMES", limit: data.limit });
};
computePort.onmessage = ({ data }) => {
  storagePort.postMessage({ type: "SAVE", snapshot: data });
  uiPort.postMessage({ type: "RENDER", state: data });
};

In practice

// main.js: small bootstrapper that wires the actor graph once
const state = new Worker("/actors/state.js", { type: "module" });
const search = new Worker("/actors/search.js", { type: "module" });
const db = new Worker("/actors/opfs-store.js", { type: "module" });

function connect(a, nameB, b, nameA) {
  const { port1, port2 } = new MessageChannel();
  a.postMessage({ hookup: nameB }, [port1]);
  b.postMessage({ hookup: nameA }, [port2]);
}

const uiToState = new MessageChannel();
state.postMessage({ hookup: "ui" }, [uiToState.port2]);
connect(state, "search", search, "state");
connect(state, "db", db, "state");

// UI actor only dispatches user intent and paints pushed view models
searchInput.oninput = () => uiToState.port1.postMessage({ type: "QUERY", q: searchInput.value });
uiToState.port1.onmessage = ({ data }) => renderResults(data.hits);

How it works

The standard way people add a Web Worker is hub-and-spoke: the main thread creates Worker A and Worker B, receives a result from A, copies it into a message to B, and waits again. Every handoff stalls whenever the main thread is busy with layout, paint or input.

Paul Lewis and Surma popularised a cleaner model at Chrome Dev Summit 2018 (and in PolymerLabs/actor-helpers, later shipped in PROXX): stop treating workers as helper subroutines and treat the UI thread as just another actor in the graph. Each actor owns its state privately and talks exclusively by passing messages, just like Erlang or Akka.

A tiny bootstrapper runs once at startup. It creates a MessageChannel for each edge in your actor graph and transfers the ports directly into the actors (hookup() / lookup()). Once wired, the bootstrapper is out of the loop: State talks straight to Compute and Storage over direct worker-to-worker ports, with zero main-thread hops.

When Compute hits a heavy synchronous calculation — like the 450ms burn in this demo — only Compute's thread stalls. The UI actor's requestAnimationFrame loop keeps hitting 60fps because the UI thread isn't mediating worker traffic or running application logic.

Step by step

What each press of Next does in the demo above.

  1. Wire actors via hookup(). The bootstrapper creates three MessageChannels and transfers their ends directly to UI, State, Compute and Storage. From here on, the worker actors talk peer-to-peer.
  2. UI dispatches SOLVE. UI sends SOLVE(25,000) to State. State delegates directly to Compute, persists the result directly to Storage, and pushes a view model back to UI with zero main-thread hops between workers.
  3. Block Compute Actor. Compute burns 450ms synchronously on its own thread. Watch the UI Actor's requestAnimationFrame counter keep ticking smoothly without dropping frames.
  4. Dispatch second action. Once Compute unblocks, a second job runs cleanly through State, Compute and Storage, writing snapshot v2.

What you can build with it

Other ideas

  • Offline-first productivity apps where a State actor coordinates a search-index Compute worker and an OPFS/IndexedDB Storage worker without waking the UI thread.
  • Games and simulations (like PROXX) where board logic, AI/solver work and UI rendering run as decoupled peers so input never janks.
  • Client-side AI apps where an orchestration actor routes between a WebGPU/WASM inference worker, a local vector store worker and a thin UI actor.

Things to try

  • Wire the actors first and watch the three port pills fly into place before the cables snap in.
  • Dispatch SOLVE(25,000) and check the UI Actor box: State, Compute and Storage complete the whole chain with 0 main-thread hops.
  • Press Block Compute Actor for 450ms and keep your eyes on the UI frames counter — it never pauses while Compute is frozen.

Gotchas

  • Don't create an actor per tiny module. Every extra Web Worker carries real startup memory (often several megabytes per thread) and structured-clone serialisation overhead — group cohesive domains into 2 to 4 coarse-grained actors.
  • Debugging distributed async flows is genuinely harder than stepping through one synchronous store. Attach a correlation ID or trace log to actions early so you can reconstruct message order.
  • Actors don't automatically notify peers when a worker crashes or terminates. Pair long-lived actor links with timeouts or health checks if an actor can fail independently.
  • Because workers can't touch the DOM, keep the UI actor's view model flat and cheap to serialise so rendering diffs don't blow your frame budget on the main thread.

References

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

MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
IndexedDB
A database in the browser that belongs to one origin. It can store most things that can be posted, including a CryptoKey. MDNIndexed Database API
Web Workers
Scripts that run on their own thread, away from the page. MDNHTML Standard
Origin private file system
Origin-scoped file storage hidden from the user's normal filesystem. A worker can open a file in it for fast, exclusive synchronous access. MDNFile System Standard

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