Ga naar de inhoud
InstaPlay
Onderdelen van de SDK-documentatie

Cloud saves

A save is JSON under a key (^[A-Za-z0-9_.-]{1,64}$) per player and game. Limits: a key ≤ 512 KB, at most 32 keys and 1 MB per game.

Reading

const entry = await InstaPlay.saves.get('progress')
// null — no such key; otherwise { data, version, updatedAt, pending, confirmed }
const many = await InstaPlay.saves.getMany(['progress', 'settings'])
  • pending: true — the SDK has a local version not yet confirmed by the server (get returns it).
  • confirmed: true — the server answered during this page load. confirmed: false — the cloud did not answer (no network, no session) and get returned the copy from the device. You can play from it: a write over the copy succeeds if the cloud has not changed since, and if it has, the cloud wins (see "Conflicts").

null means "the server has no such key". If the SDK does not know that — the server did not answer, there is no network or the game runs without a session (the portal could not create one), and there is no local copy of the key — get rejects (unavailable, offline, timeout) instead of returning null:

let progress
try {
  progress = (await InstaPlay.saves.get('progress'))?.data ?? defaults
} catch (e) {
  progress = defaults // the cloud did not answer: play on, a write will not overwrite the cloud
}

Writing

await InstaPlay.saves.set('progress', { level: 7, coins: 120 })

InstaPlay.saves.set(key, data, options?) writes to the local buffer on the game origin immediately and sends to the server with a 1 s debounce (immediately when the tab is hidden). Options:

Option Effect
expectedVersion the version you overwrite (default — the last version the SDK knows)
force overwrite any version (the previous one goes to a 30-day archive). Applies only if during this load the SDK got from the server the key version the write grew from; otherwise the write is sent without force
immediate send without the debounce

The promise resolves with { version, updatedAt } after the server answers. Errors:

  • data errors — invalid_payload, payload_too_large, quota_exceeded, message_too_large — are not retried: this data is dropped from the buffer (the next set is sent from the same server version);
  • everything else (offline, unavailable, timeout, rate_limited, internal_error and others) — the data stays in the buffer and is sent automatically: retries 1 → 60 s (at most 20 per page load), the online event, hiding the tab, flush, the next init;
  • without a session set writes to the buffer and rejects with unavailable — the data is sent on the next visit with a session.

Data must be JSON without \u0000 and lone surrogates in strings and keys (the storage rejects them): set rejects with invalid_payload at once. Compression like LZString.compress produces such strings — use compressToBase64/compressToUTF16.

InstaPlay.saves.flush() — send the buffer now (for example, before leaving a level).

Write budget

Calling set on every state change is the normal path: the 1 s per-key debounce merges frequent writes of one key into one send, and hiding the tab or a pause sends the buffer at once. The per-tab budget is 60 sends and 4 MB a minute across all keys; above it — rate_limited, the data stays in the buffer and goes out on retry. So:

  • do not use immediate: true or flush() on every frame or move — only for what matters (a purchase with in-game currency, the end of a level, leaving the game);
  • split state into keys by how often they change (settings rarely, progress often) instead of writing the whole world into one key on every action;
  • do not store what can be recomputed (caches, derived data).

Removing

await InstaPlay.saves.remove('progress')
await InstaPlay.saves.clear() // all keys of the game

Without a session remove and clear reject with unavailable and touch nothing — neither the cloud nor the buffer.

Conflicts

Two tabs or devices wrote the same key — the server answers version_conflict. Who wins by default depends on whether the server confirmed, during this page load, the version the local one grew from:

  • the base is confirmed (during this load the game read the key with get, including a null answer, or a write was already confirmed) — the local version wins (it was just played): the SDK retries with force, and the server version goes to the archive for 30 days;
  • the base is not confirmed (a copy from a previous load or another tab, get was not called or failed, the game ran without a session) — server data is never destroyed: if the cloud has no such key, the local version creates it; if the cloud has changed, the server wins: the set promise rejects with version_conflict, get returns the server version, and the local one stays for debugging in the game origin localStorage under __ip:orphan:<key> (the latest version, for 30 days; support cannot see it).

Two tabs of one browser. The save buffer is shared by the tabs of the game origin, while the version a key is written from is remembered by each page load itself — the one it last received from the server (get, a confirmed write). If tab A wrote the key after tab B had read it, B's write goes from its own, stale version and loses to the cloud: the set promise rejects with version_conflict, B's data goes to __ip:orphan:<key>, A's progress is intact. Handle the rejection as after any loss: read the key again (get) and redraw the game — the game template does it itself (the stale status). Two offline tabs write one buffer — the last action wins.

After losing on send, the key "must be read": until the game calls get/getMany for it, set rejects with version_conflict (the data goes to __ip:orphan:<key>). Read the key again and redraw the game.

Unsent data is bound to the player: if another guest opens the game in the same browser, the foreign buffer is neither sent nor returned to the game — it also goes to __ip:orphan:<key>.

Your own handler is called in both cases (remote.confirmed is always true):

InstaPlay.saves.onConflict(({ key, local, remote }) => {
  return remote && remote.data.level > local.level ? remote.data : local
})

InstaPlay.saves.onConflict(null) restores the default behaviour. If the handler throws or returns invalid data, the SDK applies the default behaviour.