What you can do with a MessagePort
Hello, I'm Paul Kinlan. I've been working on the web for a long time, and I think message channels and message ports are massively underserved. Most people don't know what they can do.
You can hand a MessagePort to another window, another origin or a worker, and they can pass it on again. Whoever holds it can talk to the other end, and nothing in between has to stay open.
I made this site while exploring where the web meets AI. It explains how ports work, with demos you can run and code you can copy. I also write about the web and AI at aifoc.us.
Three windows on three different sites. Site A and Site B have never met. Press Next to connect each to the introducer, introduce them directly, let them talk, then close the introducer.
Experiments
Everything here runs in your browser, across separate origins. The basics come first. The last five are bigger experiments: four that use the hub, and the AI router.
-
The six basic patterns
A channel, an introduction, reply ports, giving away less than you have, a SharedWorker exchange and streams. Each one runs on the page.
-
What else a port can carry
A canvas that another origin paints into, a function lent across origins, 64 MB moved in a few milliseconds, and a stream pipeline across three origins. There is also a table that tests which of twenty-three kinds of object your browser will send down a port.
-
Where a port can end up
Tear a widget off into a new window and it carries on working. Make one tab the web server for the other tabs. Leave a port with a SharedWorker and collect it after a reload.
-
Workers, video and signing keys
Two workers that keep talking while the page is frozen, live video filtered on another origin, a signing key that is used through a port and never read, and a worker that blocks on a port until you answer.
-
Between real sites: sending keys, streams and connections
Here the other side is a real site with its own storage. Send it a signing key, one side of a stream, your data to transform and send back, or a network connection to borrow.
Send a keySend one side of a streamPipe through another siteLend a connectionA port that only goes one way
-
Other devices, service workers, sockets and audio
A port from your phone to a frame on your laptop, another site mounted at a path on this one, one server connection shared by every tab, and a worker that plays music straight into the audio thread.
A port to another deviceMount a site at a pathOne socket for every tabThe audio thread
-
The lab: what can be sent where
Forty-five kinds of object sent to thirteen places a message can go, including every sort of worker, the audio thread, and frames on this origin, another origin and no origin. Your browser's results are added to everyone else's, so the table shows where browsers disagree.
-
The guide
All of this written down in one place: where a message can go, what can be sent, ports as permissions, streams, threads, keys and the things that go wrong. Every claim links to an interactive demo.
-
Web Intents, with a port at the end
A site says what it needs in plain words and you choose which site answers. The picker opens that site, gives each side one end of a channel and closes. The call then runs on your machine, even with the network off. The same tools are offered to WebMCP (Web Model Context Protocol).
// a site with something to offer serve({ name: "Stem and Petal", tools: [{ name: "book_delivery", description, inputSchema, execute }] }); // a site that needs something, and has never heard of the florist const line = await pick("send flowers to a friend"); await line.call({ bouquet: "ember", recipient: "Ada" });
-
One registry of every site's tools
Sites send their tools to your hub (a small server that is yours) each time they load, so the picker can list them without any tab being open. A site you trust can ask for the whole registry and gets a Model Context Protocol connection over one port.
-
Your agent, on any website
Tested with a stand-in agent
A site asks for an agent and gets a port that speaks the Agent Client Protocol (ACP). You choose the agent by running the bridge next to Claude Code, Gemini CLI or anything else that speaks ACP. The agent gets the registry too, so it can use tools in your open tabs. If you set nothing up you get a built-in agent that has no model and only matches words.
-
Links that carry an intent
A link whose address is a request:
<a href="intent:find a book about raising chickens">. Clicking it asks your hub who can do that. -
An AI router: use your own model account on any site
A site asks for an AI model. A window from the router opens and you pick one of your own accounts. The site gets a port to send prompts on and never sees your API key. You can see what each site used and stop it.
What I've learned so far
- A hidden hub iframe doesn't work any more. Browsers partition a SharedWorker in a cross-site iframe by the top-level site, so each site would get its own empty hub. The hub has to be a window of its own, which is the old Web Intents picker, and that gives you somewhere to say yes.
- The hub is only needed for the introduction. A port handed over through that window keeps working after the window closes, and with the network off.
- Keep the directory on a server. A port only lives while both pages do, so the list of who offers what is kept on a server, and the picker opens the tool's page when you choose it.
- Tokens stay in each site's own storage. A site keeps its link to your hub in its own storage, which partitioning doesn't touch. The server only accepts it from that site's origin.
- An agent on a website needs a gatekeeper in the middle. The hub rewrites the session so a site can't pick the agent's folder, give it servers or see another site's session.
- Introduce with a port before sending a stream. A stream relayed through an introducer stalls silently when the introducer goes away. A port doesn't.
- A port can survive its page reloading. Post it to a SharedWorker made with
extendedLifetimeas the page unloads and collect it on the next load. - Unrelated tabs need a WebRTC bridge. Two unrelated tabs can't pass a port directly. When you pick a tool whose site is already open in a tab, your hub server introduces the two tabs and they open a WebRTC channel, which carries JSON only (no ports or streams).
Where this comes from
- Web Intents, service discovery for the web
- Reinventing Web Intents, a middle-man that returns a port
- Calling APIs from the browser shouldn't mean exposing keys
- WebMCP is the new Web Intents ... maybe
- I think I've got it, and webmcp-relay
- Extensible web apps