Embedding Chess Twist

Chess Twist runs on your page in an iframe served by polyreplay.com. Your players play each other live: one sends an invite link, the other opens it. Players sign in with a polyreplay account, so the iframe is all you add.

Your iframe address:

https://polyreplay.com/embed/YOUR-NAME/chesstwist

YOUR-NAME is the short name we set up for you. We send it when you sign up.

1. Add the iframe

Paste this where the game goes:

<iframe id="chesstwist"
        src="https://polyreplay.com/embed/YOUR-NAME/chesstwist"
        allow="fullscreen; clipboard-write; web-share"
        style="width: 100%; height: 100vh; border: 0"></iframe>
<script>
  // Invite links open this page with ?room=CODE. Hand it on to the game.
  var room = new URLSearchParams(location.search).get("room");
  if (room) {
    var iframe = document.getElementById("chesstwist");
    var url = new URL(iframe.src);
    url.searchParams.set("room", room);
    iframe.src = url.toString();
  }
</script>

Then tell us the address of the page that holds the iframe, for example https://games.example.com/chess. Invite links point to it with ?room=CODE added.

2. Two questions for you

Who signs your players in?

  • We do. A player who sends an invite signs in to a polyreplay account with Google, Apple or email, in a small window. Nothing to build.
  • Your server does. Your players use the accounts they already have on your site. This needs a developer to add a few lines to your server (section 3). We help with it when we set you up.

May guests send invites, or only accept them?

A guest is a player without an account who picks a nickname. Accepting an invite is always open to guests. Sending one is off unless you ask us to turn it on.

3. Your own login, for your developer

Only if your server signs your players in. Three steps:

  1. Keep the signing secret we send you on your server. Anyone who has it can sign in as any of your players. If it leaks, ask us for a new one.
  2. On each page render, make a token for the signed-in player with the function below.
  3. Put it in the iframe address as t=. With an invite, keep both: ?t=...&room=CODE.

Ruby

require "openssl"
require "base64"
require "json"

def chesstwist_token(secret, player_id, name)
  payload = Base64.urlsafe_encode64({ sub: player_id.to_s, name: name, exp: Time.now.to_i + 600 }.to_json, padding: false)
  signature = Base64.urlsafe_encode64(OpenSSL::HMAC.digest("SHA256", secret, payload), padding: false)
  "#{payload}.#{signature}"
end

src = "https://polyreplay.com/embed/YOUR-NAME/chesstwist?t=" + chesstwist_token(secret, user.id, user.name)

PHP

function base64url($bytes) {
    return rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
}

function chesstwist_token($secret, $playerId, $name) {
    $payload = base64url(json_encode(['sub' => (string) $playerId, 'name' => $name, 'exp' => time() + 600]));
    $signature = base64url(hash_hmac('sha256', $payload, $secret, true));
    return $payload . '.' . $signature;
}

$src = 'https://polyreplay.com/embed/YOUR-NAME/chesstwist?t=' . chesstwist_token(getenv('CHESSTWIST_SECRET'), $user->id, $user->name);

Node

const crypto = require("node:crypto");

function chesstwistToken(secret, playerId, name) {
  const payload = Buffer.from(JSON.stringify({ sub: String(playerId), name, exp: Math.floor(Date.now() / 1000) + 600 })).toString("base64url");
  const signature = crypto.createHmac("sha256", secret).update(payload).digest("base64url");
  return `${payload}.${signature}`;
}

const src = "https://polyreplay.com/embed/YOUR-NAME/chesstwist?t=" + chesstwistToken(process.env.CHESSTWIST_SECRET, user.id, user.name);

Python

import base64, hashlib, hmac, json, os, time

def base64url(data: bytes) -> str:
    return base64.urlsafe_b64encode(data).rstrip(b"=").decode()

def chesstwist_token(secret: str, player_id, name: str) -> str:
    payload = base64url(json.dumps({"sub": str(player_id), "name": name, "exp": int(time.time()) + 600}).encode())
    signature = base64url(hmac.new(secret.encode(), payload.encode(), hashlib.sha256).digest())
    return f"{payload}.{signature}"

src = "https://polyreplay.com/embed/YOUR-NAME/chesstwist?t=" + chesstwist_token(os.environ["CHESSTWIST_SECRET"], user.id, user.name)

The token format

Tokenbase64url(payload) + "." + base64url(signature)
PayloadJSON: {"sub": ..., "name": ..., "exp": ...}
SignatureHMAC-SHA256 with your secret, over the base64url text of the payload
Encodingbase64url, padding optional
subYour player id, as a string, 128 characters at most
nameThe name other players see. Longer than 40 characters is cut
expExpiry in unix seconds, at most 10 minutes ahead. Make a fresh token on every render

The game swaps your token for one of ours as it loads, so the player stays signed in for the whole visit.

If the game does not load or sign-in does not come back

  • Check that the page is on a site we set up for you. The iframe loads only there. For a new site, send us its address.
  • Remove any sandbox attribute from the iframe. Some site builders add one on their own. If you need it, give it at least allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox allow-forms.
  • If your page sends a Cross-Origin-Opener-Policy header, set it to same-origin-allow-popups. With same-origin, the sign-in window cannot return to the game.