← Developer console

Service API v1 · Host contract 1

Integration contract

Your game keeps its own sign-in and its own match servers. jokio holds the player record that outlives a session: profile, friends, stats, leaderboards, achievements and purchases.

1. Authenticate your server

Register a game in the console. You get a game id and a secret for the live game and for its sandbox. Your server sends them with HTTP Basic auth on every call under /v1/server: the game id is the username and the secret is the password. Keep the secret on your server.

Authorization: Basic base64("g_…:jk_…")

2. Resolve your user to a player

After your own sign-in, call resolve with your user id. The first call creates the player and the link; later calls return the same player id, even when several arrive at once. The answer carries what jokio holds about the player for your game: profile, stats, achievements and entitlements.

POST /v1/server/players/resolve
{ "externalUserId": "your-user-id", "profile": { "nickname": "Ada" } }

3. Give your client a player token

Ask for a 15-minute token for the player and hand it to your client. The client SDK uses it to create and join rooms, read friends and report presence.

POST /v1/server/players/:id/token

Get the client SDK

The client SDK is served by jokio itself; it is not on npm. In a page without a build step, import it by its address. That address always answers with the latest version 1 SDK, so fixes reach your game without a change on your side:

import { JokioClient, connectToHost } from 'https://jok.io/sdk/v1/jokio-client-sdk.js'

In a project with a bundler or TypeScript, install version 0.5.1 with its types from its address and import @jokio/client-sdk:

npm install https://jok.io/sdk/jokio-client-sdk-0.5.1.tgz

An installed version never changes; to update, install the address of the newer version.

4. Matches

  1. Define leaderboards and achievements on your game's page in the console.
  2. A player creates a room with the client SDK and invites friends, or joins a room they were invited to. Every room also has a 6-character code (for example 8UX9YW) that its players can share: anyone who enters it on your site joins that room through joinRoomByCode, friend or not. Players join and leave rooms only through jokio; your server never seats anyone.
  3. Each client learns its room id and connects to your server with its player token. Verify the token against the keys at /v1/keys to learn who is connecting, then read GET /v1/server/rooms/:id to check that player is in the room. jokio never calls your server.
  4. Keep the match state on the room: read it with GET /v1/server/rooms/:id/data and write it with PUT /v1/server/rooms/:id/data (up to 256 KB). Only your server can see it, and it is deleted when you report the result. A read returns a revision; send it back as expectedRevision and the write is refused with revision_conflict if another of your servers wrote in between, so read again and retry.
  5. Start the match with POST /v1/server/rooms/:id/start once every player has told your server they are ready. The room must be full. jokio marks the room started (a room is waiting until then; both come with every room as status), sends room.started with the room to each of its players, and no longer lets anyone join it. Calling it again does nothing.
  6. Tell the players what happened with POST /v1/server/rooms/:id/messages: up to 16 KB of your own JSON in data, sent to everyone in the room or only to the playerIds you name. Your frame receives it from its host as a game.message event, and the page hosting the frame receives it too. Messages are not stored, so a frame that starts or reconnects asks your server for the current state. If your server keeps its own connection to its players you do not need this.
  7. Report the finish once with POST /v1/server/rooms/:id/result: each player's outcome, score and metrics, plus stat changes, achievement progress and rewards. It takes an idempotencyKey; a retry returns the first result. Leaderboards are worked out from these results every few minutes, for the last 24 hours, 7 days and 30 days; you never post scores.

These five calls are the whole server API.

5. Run inside jokio

jokio loads your client from your own origin in a sandboxed frame and talks to it with messages. Ship one bundle: the same messages come from your own site's shell.

host → game   init { host, apiUrl, playerToken, roomId, locale, theme }
host → game   token { playerToken, playerTokenExpiresAt }
host → game   room { roomId }
game → host   ready · matchEnded · requestFullscreen · rematch · leave · abandon · openProfile · openStore · error

The frame fills everything under the jokio top bar. To take the whole screen instead, set "Play screen on jokio" to full screen in your game's settings. Either way the buttons shown after a match are yours, in your game's style. Send rematch when the player asks to play again and leave when they want to go back to the lobby: the host asks for the rematch, or closes the frame on leave. leave takes the player out of a room that has not started; once the room is started it changes nothing, so the player stays in the match and can come back to it from the lobby. A player can also abandon a started match: from jokio's room panel, or from your game by sending abandon to the host (abandonRoom if your own site is the host). Your server has no call for it. The player stays in playerIds and is added to abandonedPlayerIds, the room is gone from their rooms and they cannot return to it. jokio does not call your server about it: your client is told through link.onPlayerAbandoned, and your server sees abandonedPlayerIds whenever it reads the room. Clocks, timeouts and bots are yours: keep the deadline in the room data, check it on every request, and let the waiting client ask your server when it passes. After the result, leave closes the rematch.

Every message carries jokio: 1, the contract version. Verify player tokens against the keys at /v1/keys.

6. One player across games

When you resolve a user, also send how they signed in to your game: identities with a Google account id or an email. What happens next depends on whether jokio trusts your game's sign-in, which only the platform owner can switch on.

  • Trusted: a user with the same Google account or email as an existing player is that player straight away, across games and on jokio itself.
  • Not trusted (the default): your users get their own player. When that person signs in to jokio with the same Google account or email, jokio offers them the progress and merges it only if they confirm.

7. Events

jokio sends events only to players, never to game servers. A game client receives them through the client SDK's subscribe, which connects the player to the realtime service with their player token: friend and presence changes, invites, room allocated and ended, rewards, and the room messages your own server sends (those sent to the whole room arrive while the room is hosted with hostGame or watched with watchRoom). Your server asks the API whenever it needs something.

After a match, rematch asks for another one with the same players. Each time someone asks or backs out, every player of the match receives room.rematch with who asked and the players who are ready. When the last one is in, they all receive room.reset with the new room: hostGame keeps the same frame, passes the event on and follows the new room, so your game client starts the new match in place with the room id from the event.