Running software inside somebody else's software
On execution boundaries, distributed ownership and the architecture of embeddable applications.
I've had to build software that lives inside other people's software a surprising number of times throughout my career. Checkout at Fast, authentication at Magic, music embeds at
Audiomack and, more recently,
Handshake.
The interesting thing about building an embeddable application is that you relinquish control over a considerable portion of its execution environment.
You don't own the document. You don't own the navigation. You don't necessarily know what framework the host application uses.
An solves some of these problems whilst introducing an entirely different category of them.
The iframe is the glass box
One of the architectural decisions I made with Handshake was that the host owns the geometry and the widget owns the pixels.
The iframe can request a change to its dimensions, but the SDK decides what those dimensions actually become.
This distinction becomes particularly interesting when implementing a draggable interface. The pointer exists inside the iframe, but the element we're trying to move exists outside it.
Using and clientY introduces a moving coordinate system. As the host moves the iframe, the pointer's coordinates relative to that iframe change.
Instead, the widget communicates .
const dx = screenX - dragStart.screenX;
const dy = screenY - dragStart.screenY;
position = clampPosition({
left: dragStart.pos.left + dx,
bottom: dragStart.pos.bottom - dy
});
The same distinction applies to responsive behaviour. An iframe that's 400 pixels wide doesn't necessarily exist on a mobile device.
The widget knows the dimensions of its own viewport. The host knows the dimensions of the device's available viewport. These are two entirely different pieces of information.
In Handshake, the host communicates that distinction explicitly.
Readiness is a protocol, not an event
There are several points at which an embedded application might appear ready.
The SDK has loaded. The iframe exists. The document has loaded. React has mounted. The message listener has been registered.
None of these independently guarantees that the application can receive and process a command.
An early implementation of Handshake would occasionally send messages whilst the iframe was still navigating away from about:blank. Those messages were effectively sent into the void.
The eventual solution was to introduce an application-level readiness handshake.
private deliverToWidget(message: HostToWidget) {
if (!this.iframe || !this.widgetBridge) return;
if (this.iframeReady) {
this.widgetBridge.send(message);
} else {
this.pendingWidgetMessages.push(message);
}
}
The widget registers its listeners and explicitly announces READY. The host then flushes its queue.
We subsequently introduced a watchdog for readiness and another for session initialisation. Receiving a READY message doesn't mean the widget has successfully established a session.
A second READY is particularly interesting. It could mean the embedded application has remounted and lost its in-memory state. The host might still believe a session exists that the iframe no longer knows anything about.
Readiness isn't necessarily a permanent state.
Accidentally building RPC
Initially, communication was predominantly fire-and-forget.
iframe.contentWindow?.postMessage(message, origin);
But once you start caring about whether a message arrived, whether the receiver understood it and whether something actually happened, becomes insufficient as an application protocol.
You need identifiers, correlation, timeouts and a representation of failure.
Eventually, Handshake's bridge evolved into typed RPC.
The widget exposes a collection of methods. The host exposes a notification interface. Both operate through the same underlying transport.
A simplified representation:
interface WidgetRpc {
beginSession: (options: SessionOptions) => Promise<void>;
requestClose: () => Promise<void>;
}
interface HostRpc {
notify: (event: WidgetNotification) => Promise<void>;
}
Underneath that interface is a collection of pending requests, keyed by unique identifiers.
One particularly important distinction is that acknowledging a request doesn't necessarily mean completing its associated operation.
A request to begin a session can be acknowledged before the asynchronous session initialisation finishes. The business-level result arrives separately.
There are effectively three questions: did the request arrive, was it accepted, and did the operation succeed?
Representing all three with a single boolean is an invitation for some particularly unpleasant debugging.
The boundary is the architecture
Even something as ordinary as closing a drawer becomes a coordinated operation.
The iframe owns the content transition. The host owns the transition of the iframe's dimensions.
In Handshake, the widget announces that its content has finished exiting. The host subsequently collapses the container and communicates the dismissal back to the widget.
Neither side can independently complete the entire interaction.
Pip
Hi! Fancy the overshirt? Make me an offer.
Pip
Hi! Fancy the overshirt? Make me an offer.
The same boundary determines what each side should trust.
TypeScript can describe the contract at compile time, but a message arriving from another execution context is still runtime data. , source validation and payload validation are separate responsibilities.
An iframe provides isolation. It doesn't provide authorisation for the operations you expose across it.
After building a few of these systems, I've found that the most consequential architectural decisions rarely concern the iframe itself.
They concern the contract between two applications that happen to occupy the same screen.
It's quite amusing how much distributed systems engineering can hide inside what somebody else experiences as a little floating button.
— Adenekan
