Port Authority

Port patterns / The four primitives

Connect two sites to each other, then remove yourself

A MessagePort can be sent down another MessagePort. Two unrelated browser windows can't just connect to an introducer out of thin air — a page only gets a handle to another window via window.open() (as in the chain below: Site A opens the Introducer, which opens Site B) or by embedding two <iframe> elements directly (see wiring two widgets on one page). Once the Introducer holds a port to each side, it creates a fresh MessageChannel, transfers port1 down its port to Site A and port2 down its port to Site B, and closes. The direct line keeps working without it.

When you'd use this

A print shop wants photos stored on a third-party photo site it doesn't have an integration with. When the user clicks Choose photos, the shop calls window.open() on a picker window and hands it a port. The user picks their photo provider, so the picker calls window.open() on that provider, transfers a fresh channel's ends down both ports (or forwards a caller-minted port untouched), and closes. If both sites are already embedded in <iframe> elements on one host page, the host page wires them directly without any popups.

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.

Step by step

What each press of Next does in the demo above.

  1. Site A opens Introducer. Two unrelated tabs can't just connect to an introducer. Site A calls window.open() on the Introducer, makes a MessageChannel, and posts one end over that window handle.
  2. Introducer opens Site B. Now the Introducer calls window.open() on Site B and sends it a port of its own. The Introducer has a port to each side, while A and B still can't reach each other.
  3. Pass port1 & port2 over those ports. This is the key trick: the Introducer makes a fresh MessageChannel and transfers port1 down its port to Site A and port2 down its port to Site B. Watch the yellow ports ride over the existing cables, then snap into a direct channel.
  4. Site A says hello. Site A talks straight to Site B on port1 ↔ port2. Nothing goes through the Introducer.
  5. Site B says hello. Site B replies directly down port2 on the same yellow channel.
  6. Close the introducer. The Introducer closes and both of its dark cables vanish. The yellow direct channel stays alive.
  7. Site A says hello. Site A still reaches Site B. Once port1 and port2 were handed over, the Introducer was out of the loop.

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.

  1. Wire the two widgets together. Each widget gets one end of a channel. This page keeps neither.
  2. List: pick the next place. The list tells the map directly. The map answers the list.
  3. 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.

  1. Site A creates channel & opens Introducer. Site A mints new MessageChannel(), keeps port1, opens the Introducer, and transfers port2 over.
  2. 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.
  3. 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!
  4. Site B replies directly. Site B replies straight down port2 on the yellow direct channel.
  5. Close the introducer. The Introducer closes; the direct channel stays alive.
  6. 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.

  1. 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.
  2. User: pick Petal Post. The picker creates a channel and transfers port1 to Petal Post and port2 to the shop over their existing ports.
  3. Close the picker. The picker's frame is destroyed — the direct line between the shop and Petal Post stays alive.
  4. Shop: order twelve tulips. The order travels straight to Petal Post and the reply comes straight back. Rose and Thorn hears nothing.

Things to try

  • Step through opening the Introducer, opening Site B, and passing port1 & port2 over the existing ports. Watch the yellow badges ride down both dark cables before snapping into the direct line.
  • Try caller-minted forwarding below and send a message into port1 before Site B even opens.
  • Close the introducer before making either site speak, then press Site A says hello: the direct yellow line still delivers.

Gotchas

  • You can't connect two already-open cross-origin tabs out of thin air. Every first port has to cross an existing handle from window.open() or an embedded <iframe>.
  • Opening a popup introducer requires user activation (a click), and a strict Cross-Origin-Opener-Policy header severs window.opener. Give posted ports a tick to queue before calling window.close().
  • Once you give away both ends, you can't revoke the direct channel. If you need revocation, stay in the middle and forward — see attenuate.
  • Neither side gets an event when the other closes its port. Use a health port or a same-origin Web Lock if you need liveness.

Measured

Chromium 157: messages keep flowing between two frames on different sites after the introducing frame, on a third site, is removed from the document. 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.

MessagePort
One end of a message channel. Whatever is posted into it comes out of the other end, and it can be sent to another page, frame or worker. 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
MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
BroadcastChannel
A named channel that every page and worker on the same origin can join. A message goes to all of them. It cannot carry ports or other transferable objects. MDNHTML Standard
SharedWorker
A worker that every page on the same origin can connect to. Each page that connects gets a port to it. MDNHTML Standard
Web Locks
Lets pages and workers on one origin take a named lock, so that only one of them does a piece of work at a time. MDNWeb Locks

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