Connecting and master
The connect URL
wss://ws.riffserver.com?appId=<appId>&playerId=<playerId>&playerName=<name>[&token=<token>]
| Parameter | Required | Notes |
|---|---|---|
appId | yes | Your app's id. Unknown or disabled apps are refused. |
playerId | yes | Your stable id for this player. Keep it the same across sessions if you want to recognise them. |
playerName | no | Display name shown to others. |
token | only if the app requires signed tokens | See below. |
The connection is accepted or refused during the WebSocket handshake. A refused connection fails to open; there is no message explaining why. Your dashboard lists the reason for refused connections (for example "bad or missing token").
What can refuse a connection
In this order:
- Missing
appId/playerId. - The app does not exist or is disabled.
- The app is at its connection cap (HTTP 429).
- The app requires signed tokens and the token is missing, expired, or wrong.
- The app has an auth webhook and it says no (or is unreachable).
Signed tokens
Turn on Require signed token for the app in your dashboard. You get a signing secret; keep it on your game backend, never in the client. For each player your backend creates a token:
token = "<exp>.<hex hmac_sha256(signingSecret, appId + "\n" + playerId + "\n" + exp)>"
exp is a Unix timestamp in seconds. Tokens are bound to one appId and one playerId, so a token for one player cannot be reused for another. Keep lifetimes short (minutes); the client only needs the token to open the connection.
Example (Node):
const crypto = require('crypto');
function connectToken(signingSecret, appId, playerId, ttlSeconds = 300) {
const exp = Math.floor(Date.now() / 1000) + ttlSeconds;
const mac = crypto.createHmac('sha256', signingSecret).update(`${appId}\n${playerId}\n${exp}`).digest('hex');
return `${exp}.${mac}`;
}
The dashboard's Keys & tokens page has a test token generator for trying this out.
Auth webhook (optional, separate)
An app can also set an auth URL. After the token check, Riff POSTs { appId, playerId, playerName, token } to it and expects { "success": true }. Use it to check a player against your own accounts. It is independent of signed tokens.
Keeping the connection alive
The Unity SDK sends a ping every 30 seconds and expects a pong within 5 seconds, closing and reconnecting if not. If you write your own client, do the same; idle WebSocket connections are closed by the platform after a while.
Disconnecting
When a connection closes (cleanly or not) Riff removes the player from their room, tells the others (playerLeft), hands over master if needed, and deletes the room if it is empty. If the SDK has autoReconnect on, it reconnects with the same playerId, but the player is not put back in their room. Your game decides whether to call joinRoom again.
The master (host)
- The player who creates a room is its master.
- If the master leaves or disconnects, the player who has been in the room longest becomes master, and everyone receives
hostChanged { newHostConnectionId }. - Masters can do three things nobody else can:
setRoomProperties,setTurn, andendGame. Others getNOT_HOST. - Any player can message the master directly with
sendToMaster. See Messaging and events. - The master is identified by connection id, not player id. In Unity:
client.IsMasterClient.
Typical pattern: the master is authoritative for game rules (it receives sendToMaster requests, decides, and broadcasts the outcome with sendToRoom).