The player must already be inside a realtime-enabled room. The response carries a single-use-window token (valid 5 minutes) and the server coordinates to connect to.
$ curl -X POST "/api/realtime.php/token" \
-H "X-Api-Token: API_TOKEN" \
-H "X-Game-Player-Token: PLAYER_TOKEN"
{
"success": true,
"token": "REALTIME_TOKEN",
"player_info": {
"player_id": 47,
"player_name": "TestPlayer",
"room_id": "dc3723848639139113ca240958ba0bf8",
"is_host": true
},
"realtime_server": {
"host": "realtime.michitai.com",
"port": 443,
"protocol": "wss",
"udp_port": 3000
}
}
The realtime relay is a separate service — a WebSocket control lane at
wss://realtime.michitai.com:443 plus a UDP fast lane on
the port reported in realtime_server.udp_port. Mint a token
per player with the endpoint above, then authenticate the socket with the
X-Realtime-Token header. The official SDKs wrap this
handshake — see the per-language SDK pages for the client-side API.
After the handshake the server sends a connected envelope
carrying your connection_id, player info and the UDP
coordinates. All traffic is JSON envelopes:
// server -> client, once
{"type":"connected","data":{"connection_id":"…","player_id":47,"room_id":"…","udp_enabled":true,"udp_port":3000}}
// client -> server: reliable send
{"type":"send","command":"chat","target":"all","data":{"text":"hi"},"seq":1}
// client -> server: keepalive (idle sockets close ~60s; SDKs send every 20s)
{"type":"heartbeat"}
// server -> client
{"type":"receive","command":"chat","data":{"text":"hi"},"sender":{"game_player_id":47,"player_name":"P","is_host":true},"seq":1}
{"type":"sent","command":"chat","target_count":3}
{"type":"error","data":{"message":"Only the room host can send messages to other players"}}
{"type":"heartbeat_ack","timestamp":"2026-10-27T12:00:00.000Z"}
target is one of all | host | others | specific
(with target_ids for specific). Room ACL:
only the host may target all/others/specific —
non-hosts may only send to host.
Native clients (and the JavaScript SDK under Node.js) can bind a UDP
endpoint for unreliable traffic. Datagrams are MUR1-framed:
4-byte magic, 1-byte type, the 16-byte connection id, then a JSON payload.
A one-time BIND claims your address;
MSG carries send envelopes;
PING/PONG keeps NAT mappings alive. The connection id —
issued only inside the authenticated WS session — is what authenticates
datagrams, so UDP alone cannot forge a session.
Peers without a bound UDP address (every browser) still receive the room's
UDP-sourced traffic — the server delivers it over their WebSocket lane.
Browsers also cannot send UDP; sendUnreliable() in
the JS SDK falls back to WebSocket automatically. See
WebGL notes
and WebTransport.
import { MultiplayerClient } from 'michitai-multiplayer';
const mp = new MultiplayerClient(API_TOKEN);
const tok = await mp.realtime.get(playerToken);
const rt = mp.createRealtime({
onReceive: (command, data, sender) => applyMessage(command, data, sender),
onError: (message) => console.warn('rt error', message),
});
await rt.connect(tok.token, tok.realtime_server);
rt.send('all', 'chat', { text: 'hello' }); // reliable
rt.send('host', 'vote', { choice: 'a' }); // non-host allowed target
await rt.connectUdp(); // Node.js only
rt.sendUnreliable('others', 'pos', { x: 1, y: 2 }); // UDP or WS fallback