The app bridge protocol
@n5i/app-bridge implements the postMessage protocol between the dashboard (the host) and app UIs in its iframe. It has no framework dependency. If you use @n5i/ui, you don't need to use it directly: useHost() wraps it.
Installation
pnpm add @n5i/app-bridgenpm install @n5i/app-bridgeyarn add @n5i/app-bridgebun add @n5i/app-bridgeThe package's source is in the n5i dashboard repository under packages/app-bridge.
Using the client
import { applyTheme, createAppBridge } from "@n5i/app-bridge";
// Create the bridge as early as possible: it sends hello immediately.
const bridge = createAppBridge({ app: { name: "My app", version: "1.0.0" } });
const state = await bridge.connect();
bridge.on("theme", (theme) => applyTheme(theme));
bridge.on("locale", (locale) => setLocale(locale));
const jwt = await bridge.getJwt();
bridge.syncPath("/peers");
bridge.breadcrumb([{ label: "Peers" }]);
bridge.toast({ title: "Copied", color: "success" });| Method | Description |
|---|---|
connect() | Performs the handshake and resolves with the state. Safe to call more than once. |
state | Current state: embedded, connected, protocol, capabilities, contentOnly, host, hostPages, locale, colorMode, theme. |
has(capability) | Whether the host supports a capability. |
on(event, listener) | Listen to theme, colorMode, locale or state. Returns a function that removes the listener. |
getJwt(options?) | A JWT for the app, cached until shortly before it expires (minValidity, default 30 s; force to skip the cache). |
requestSession() | A sub-session the app refreshes itself. |
requestLocale() | The host's current locale. |
syncPath(path) | Report that the app navigated by itself. The host updates its URL without reloading. |
navigate(path) | Ask the host to open another path of the app. The iframe reloads. |
breadcrumb(items) | Set the page names after the app's name in the host's breadcrumb. |
toast(toast) | Show a notification in the host. |
openHostPage(name) | Open one of the host's pages (a name from state.hostPages). |
destroy() | Remove the bridge's listeners. |
Options of createAppBridge():
| Option | Default | Description |
|---|---|---|
app | none | name and version, sent to the host in hello. |
hostOrigin | origin of the referrer, else of the first message | Origin(s) of the host to accept messages from. |
capabilities | all | Capabilities to ask for. |
autoConnect | true | Send hello right away. |
handshakeTimeout | 1500 ms | How long to wait for welcome before assuming a protocol 0 host. |
requestTimeout | 15 s | How long to wait for a JWT, session or locale. |
The client only accepts messages from window.parent and, once known, only from the host's origin, and only posts messages to that origin.
Messages
Every message is an object { action, data }. Requests can carry an id, and the response carries the same value as replyTo. Unknown actions are ignored on both sides, so new messages can be added without breaking older apps or hosts.
Handshake
Protocol 1 starts with a handshake:
- The app sends
hellowith{ protocol: 1, capabilities, app? }. - The host answers with
welcome:{ protocol, capabilities, embedded, host, locale, colorMode, theme?, hostPages? }.
capabilities in welcome are the ones both sides support. embedded is true if the iframe URL has ?embedded=1.
Capabilities
| Capability | Messages |
|---|---|
session | app requestSession → host setRefreshToken { refreshToken, accessToken } |
jwt | app requestJwt → host setJwt (a JWT or null) |
locale | app requestLocale → host locale; the host also sends locale when it changes |
colorMode | host colorMode (light or dark) on load and when it changes |
theme | host theme { colorMode, highContrast, tokens } in welcome and when it changes; app requestTheme |
navigate | app navigate (a path): the host opens that path of the app and the iframe reloads |
syncPath | app syncPath (a path): the host updates its URL without reloading |
breadcrumb | app breadcrumb [{ label?, icon? }] (up to 8 items) |
toast | app toast { title, description?, color?, icon? } |
openHostPage | app openHostPage (a name from welcome.hostPages) |
Paths must start with / and stay inside the app. toast.color is one of primary, secondary, success, info, warning, error and neutral. If a request fails, the host answers with error { request, message } and replyTo, and the client's promise rejects.
Protocol 0
Hosts that don't answer hello within the handshake timeout are treated as protocol 0 hosts, which support session, jwt, locale, colorMode, navigate, breadcrumb and toast without a handshake (LEGACY_CAPABILITIES). Protocol 0 hosts require all four fields of a toast.
Apps that never send hello are protocol 0 apps. The dashboard shows them in a card, see presentation.
Authentication
App UIs often need to call the app's own API on behalf of the logged-in user. The bridge provides two ways to get credentials.
JWT (jwt capability): getJwt() returns a short-lived JWT for the app. The host creates a sub-session of the user's session for the audience app-<app id> and refreshes it as needed. The token is signed with RS512 and contains:
| Claim | Value |
|---|---|
iss | n5i |
sub | the user's username |
aud | app-<app id> |
exp | 5 minutes after issuing |
Send it to your backend (for example as Authorization: Bearer <jwt>) and verify it there with n5i's public key, which is available in templates as jwt_pubkey_base64. Check iss, aud and exp.
Sub-session (session capability): requestSession() hands the app the refresh and access token of its sub-session, and the app refreshes it itself with n5i's API from then on. After that, the host no longer issues JWTs to this app (setJwt returns null). Use this only if your app needs to keep a session independently of the dashboard.
Theme
With the theme capability, the host sends its design tokens as CSS custom properties with resolved values for the current colour mode:
{
"colorMode": "dark",
"highContrast": false,
"tokens": {
"--nv-ink": "oklch(0.95 0 0)",
"--ui-color-primary-500": "#22c55e",
"--ui-radius": "0.375rem"
}
}The tokens are the --nv-* semantic tokens of @n5i/ui, --ui-color-primary-50 to --ui-color-primary-950, --ui-color-neutral-50 to --ui-color-neutral-950, --ui-radius, --font-sans, --font-mono and --font-display.
applyTheme(theme) sets them on <html> and <body>, sets color-scheme, toggles the dark and light classes on <html> (pass colorModeClass: false if something else manages them) and the high-contrast class on <body>. It returns a function that removes everything again. Only token names matching n5i's prefixes and values without url(), ;, braces or angle brackets are applied.
Set color-scheme to the host's colour mode: an iframe whose colour scheme differs from the host's is drawn with an opaque background.
Embedded mode
state.contentOnly is true after a welcome, or if the URL has ?embedded=1. Use it to hide parts of your UI that the dashboard already provides, such as your own navigation bar or login screen.