Iframe API
Iframe API overview
The game and the page that embeds it talk with postMessage: the game sends events, your page sends commands.
Message shapes#
| Direction | Shape |
|---|---|
| Game → your page: an event | {type, data}: exactly these two keys; data is always an object ({} when the event has no fields). |
| Your page → game: a command | {cmd, data}, data only for commands that take it. |
Origins#
- The game posts only to its parent page, and only if the parent's origin is one of the brand's allowed origins. It never posts to
"*". Opened as a whole page, or framed by a page that is not allowed, it posts nothing. - The game accepts a command only from its parent window, from an allowed origin, with a known
cmdand validdata. Anything else is ignored without an answer. - Your page may post commands with any target origin; we recommend the game's origin, as in the sample below.
Amounts in messages#
Amounts in ifc.* messages are JSON numbers in major units (5.32 for 5.32 USD), a copy for your page. They carry at most 15 significant digits, cut toward minus infinity, so a number never says more than the amount; precision in ifc.init is the currency's number of decimals: 8 in every crypto currency, where release 1.0.0 sent 6 for USDT, USDC, TRX and XRP. Do not compute money from them: your wallet holds the exact figures (in units).
Listening and sending#
const frame = document.getElementById('game');
const gameOrigin = new URL(frame.src).origin;
window.addEventListener('message', (event) => {
// Only the game's origin, always; ignore everything else.
if (event.origin !== gameOrigin) return;
const { type, data } = event.data ?? {};
if (typeof type !== 'string' || !type.startsWith('ifc.')) return;
// Only the game's window. One exception: the ifc.close of a game page that
// unloads arrives after its window is gone, with event.source null.
const unloadClose = type === 'ifc.close' && event.source === null;
if (event.source !== frame.contentWindow && !unloadClose) return;
if (type === 'ifc.update.balance') showBalance(data.amount);
if (type === 'ifc.back.to.home') location.assign('/lobby');
if (type === 'ifc.close') gameClosed();
});
// A command: mute the game.
frame.contentWindow.postMessage({ cmd: 'ifc.set.sound', data: { value: false } }, gameOrigin);ifc.close when the game page unloads#
Accept a message only when event.origin is the game's origin and event.source is the frame's window. One message needs an exception to the second rule: when the game's page unloads (for example your page navigates the frame away), the game posts ifc.close while it goes away, and the browser delivers it after the frame's window is gone. Its event.origin is still the game's origin; its event.source is null.
- Accept
ifc.close, and onlyifc.close, with anullsource, and only from the game's origin, as the sample does. Every other message must come from the frame's window. Never relax the origin check. - Treat this one message as best effort: browsers do not promise to deliver a message posted while a page unloads, and when your own page goes away nothing is left to receive it. Rely on your own frame as well: your page knows when it removes or navigates the frame.
- The other causes of
ifc.close("Back to lobby", Stop in the reality-check window, the end of the session) are sent while the game's page is alive and arrive with the frame's window as source.
Only these messages#
The game posts nothing but the ifc.* events to your page and takes nothing but the ifc.* commands. It does not tell your page that it is busy or idle, that the player wants to deposit, or its size. A refused launch is shown by the game's own screen, with no message to your page.