Code
// 1. Site A called window.open(introducerUrl) and sent it a port (toA).
// 2. The Introducer called window.open(siteBUrl) and sent it a port (toB).
// 3. Now the Introducer passes a brand-new channel's ports down those ports:
const { port1, port2 } = new MessageChannel();
toA.postMessage({ meet: "Site B" }, [port1]);
toB.postMessage({ meet: "Site A" }, [port2]);
// The Introducer keeps neither end and can window.close().
In practice
// Site A (a print shop) wants photos. When the user clicks "Choose photos",
// Site A calls window.open() on a picker window and posts it a port (toA).
const picker = window.open(pickerUrl, "", "popup");
const line = new MessageChannel();
// once the picker signals ready:
picker.postMessage("line", pickerOrigin, [line.port2]); // picker keeps this as toA
// In the picker, the user chooses a photo service. The picker calls window.open()
// on that service, sends it a port (toB), creates a new MessageChannel(),
// transfers port1 down toA and port2 down toB, and closes itself.
// Back in the print shop, port1 arrives on its existing port to the picker:
line.port1.onmessage = (e) => {
const store = e.ports[0]; // port1 of the fresh direct channel
line.port1.close(); // picker is out of the loop now
show("Connected to " + e.data.meet);
store.onmessage = (m) => addToOrder(m.data.photo);
store.postMessage({ want: "photos" });
};
How it works
The introducer holds a port to two parties that have never met. In the classic model above, it creates a brand-new MessageChannel and transfers port1 down its port to Site A and port2 down its port to Site B. Because it kept neither end, it can't read anything they say and can close immediately.
There's also a second, equally useful model shown in caller-minted forwarding below: Site A creates the MessageChannel up front, keeps port1, and transfers port2 to the introducer to forward untouched when it opens Site B. That lets Site A start posting into port1 before Site B has even opened — the browser queues those messages until port2 lands.
How does the introducer get those first handles? Never by discovering random open tabs. A browser only gives one origin a handle to another in two ways: either via a window.open() chain (Site A opens the Introducer, which opens Site B), or because a parent page embeds both sites in <iframe> elements (shown in wiring two widgets on one page below).
Two top-level tabs that a user opened independently on different origins have no handle to each other, and no third-party introducer can connect them out of thin air. Same-origin tabs can coordinate through a BroadcastChannel or SharedWorker, and two sites can always relay via a server — but for direct cross-origin MessagePort channels between windows, you need a window.open() chain or a shared host page.
What you can build with it
Built and running below.
Two vendors' widgets, wired together by the page that embeds them
A host page that already embeds two third-party <iframe> widgets from different vendors and wires them directly together so they talk without bouncing messages through the host page.
A list of places from one vendor and a map from another, each in its own frame on its own origin. Neither has heard of the other and neither loads the other's code. The page that embeds them makes a channel, and gives each widget an end. Pick a place in the list and the map moves, with nothing passing through the page.
Code
const { port1, port2 } = new MessageChannel();
listFrame.contentWindow.postMessage({ cmd: "port" }, listOrigin, [port1]);
mapFrame.contentWindow.postMessage({ cmd: "port" }, mapOrigin, [port2]);
// that is all the integration code there is
Step by step
What each press of Next does in this demo.
- Wire the two widgets together. Each widget gets one end of a channel. This page keeps neither.
- List: pick the next place. The list tells the map directly. The map answers the list.
- List: pick the next place. Again. Nothing passes through the page in the middle.
Caller-minted port: Site A creates the channel and the Introducer forwards port2
A caller that creates a MessageChannel up front, starts queueing work into port1, and hands port2 to a broker to forward once the target window opens.
Instead of the Introducer creating the MessageChannel, Site A creates it up front, keeps port1, and transfers port2 to the Introducer. The Introducer then forwards that exact port2 on when it opens Site B — never reading it. Bonus: Site A can start posting into port1 before Site B even exists, and the browser queues those messages until port2 lands.
Code
// Site A creates the channel up front and keeps port1
const { port1, port2 } = new MessageChannel();
const intro = window.open(introUrl);
intro.postMessage({ cmd: "forward" }, introOrigin, [port2]);
port1.postMessage("hello (queued before B opens!)"); // buffers inside the channel
// Introducer opens Site B and forwards port2 untouched
const siteB = window.open(siteBUrl);
siteB.postMessage({ cmd: "port" }, siteBOrigin, [port2]);
Step by step
What each press of Next does in this demo.
- Site A creates channel & opens Introducer. Site A mints new MessageChannel(), keeps port1, opens the Introducer, and transfers port2 over.
- Site A sends before Site B exists. Site A posts a message into port1 while Site B is still closed — it queues safely inside the channel.
- Introducer opens Site B & forwards port2. Introducer opens Site B and forwards that exact port2 untouched. The yellow cable snaps in and Site B immediately receives the queued message!
- Site B replies directly. Site B replies straight down port2 on the yellow direct channel.
- Close the introducer. The Introducer closes; the direct channel stays alive.
- Site A sends another message. Site A still reaches Site B directly.
A picker that connects a shop to the florist you choose, then closes
A picker or hub popup opened via window.open() that opens the user's chosen service, passes a fresh channel to both sides, and closes. This site's hub and AI router both work this way.
Two florists have registered with the picker. The shop holds a port to the picker and knows neither florist. When the shop asks for someone who can send flowers, the picker lets the user choose, creates a channel, transfers port1 to the chosen florist and port2 to the shop, and closes. The order flows straight to the chosen florist while the other hears nothing. The hub on this site works the same way, with the directory stored on a server.
Code
// the shop
picker.postMessage({ want: "send flowers" });
picker.onmessage = ({ data, ports: [florist] }) => {
florist.postMessage({ order: "twelve tulips" }); // straight to the florist
};
// the picker, once the user picks a florist
const { port1, port2 } = new MessageChannel();
tools.get(choice).postMessage({ introduced: "the shop" }, [port1]);
shop.postMessage({ tool: choice }, [port2]);
// kept neither end, so it can close
Step by step
What each press of Next does in this demo.
- Shop: ask for someone to send flowers. The shop asks the picker. Two florists offer that capability, so the picker asks the user to choose.
- User: pick Petal Post. The picker creates a channel and transfers port1 to Petal Post and port2 to the shop over their existing ports.
- Close the picker. The picker's frame is destroyed — the direct line between the shop and Petal Post stays alive.
- Shop: order twelve tulips. The order travels straight to Petal Post and the reply comes straight back. Rose and Thorn hears nothing.