# postMessage reference

Source: https://mroffice.ai/en/developers/embed/postmessage

If you build your own frame host instead of using embed.js, this is the whole protocol. Frame https://mroffice.ai/<locale>/embed/v1/<app id>; wait for hello from that iframe and that origin; answer init with the token. Check event.source and event.origin on every message, and always pass the editor's origin as targetOrigin.

```text
// Every message, both ways:
{ "source": "mroffice-embed", "version": 1, "type": "<type>", "id": "<optional request id>", "payload": { … } }

// Host page → editor (targetOrigin: the editor's origin, never "*")
init          { token, theme?, ui?: { brand?, toolbar?, features? }, downloads?, sdk? }   answer to "hello"; once per boot
users         { users: [{ id, name, email?, avatarUrl? }] | null }   answer to "requestUsers" (same id); null: no directory
requestSave   {}                                             → "saved" or "error" with the same id
setMode       { mode, token }                                → "modeChanged" (a token signed for that mode)
refreshToken  { token }                                      answer to "tokenExpiring"
setTheme      { theme: "light" | "dark" | "system" }
openFile      { name, type, data: ArrayBuffer }              → "fileOpened" or "error" with the same id (transfer data; file.local tokens)
focus         {}                                             moves the keyboard focus into the document
command       { method, params, apiVersion: 1 }              the Automation API → "commandResult" with the same id
destroy       {}                                             → "destroyed"

// Editor → host page (sent to the origin that sent "init" only)
hello          { appId, boot, protocol: { version, capabilities } }
               the editor is listening; send "init". A new boot (the frame was reloaded) needs a new init, with a
               fresh token; capabilities lists what this frame can do (openFile, focus, connection, reinit, …)
awaitingFile   { accept, maxBytes, canOpen }  a file.local token, no file yet: the editor shows its drop zone
fileOpened     { name, size, type }           a file from the person's computer was opened ("ready" follows)
ready          { mode, effectiveMode, modeNote?, permissions, watermark, user,
                 document: { key, name, kind, format, version, partnerVersion }, expiresAt }
change         { dirty, modified }            dirty: edits on their way to the server; modified: since the last save
saveRequested  { reason: "user" | "host" }
saved          { saveId, unchanged, version, sha256, size, downloadUrl, downloadExpiresAt }
download       { name, handledBy, url?, mimeType?, size?, data? }   a file the editor saved (export, picture, PDF pages):
               handledBy "frame" (the browser saved it), "host" (downloads: "host": data is an ArrayBuffer, transferred,
               or url a short-lived link) or "refused" (ui.features.export is false, or the token's permissions)
error          { code, message, fatal }
modeChanged    { mode, effectiveMode }
requestUsers   { query }                      the comment box's "@": answer "users" with the same id within 4 s
usersChanged   { users: [{ id, name, avatarUrl, color, mode, since }], self }   who is in the document now
mention        { threadId, commentId, text, mentioned: [{ id, name, email }] }  a comment naming people was posted
tokenExpiring  { expiresAt }                  about 5 minutes before the session ends
connection     { online }                     the editor lost (false) or regained (true) its link to MR Office
commandResult  { ok: true, result } | { ok: false, error: { code, message, details? } }   answer to "command"
destroyed      {}
```
