JavaScript SDK — Realtime

Realtime — mp.realtime + RealtimeClient

realtime.get
await mp.realtime.get(playerToken)

Mints the realtime credential for the player's current realtime-enabled room — one live token per player, ~5-minute connect window. Returns token + realtime_server coordinates.

connect
const rt = mp.createRealtime(opts); await rt.connect(token, realtimeServer)

Opens the WebSocket control lane (wss://, client=json). Resolves on the server's connected envelope — exposes connectionId, playerInfo, udpEnabled.

send
rt.send(target, command, data, { targetIds?, seq? })

Reliable ordered message — targets 'all' | 'host' | 'others' | 'specific'. Host ACL is enforced server-side: non-hosts may only target 'host'.

connectUdp (Node.js)
await rt.connectUdp()

Binds the MUR1 UDP fast lane via node:dgram — datagrams authenticate through the WS-issued connection id. Node-only; rejects with ConfigurationError in browsers.

sendUnreliable
rt.sendUnreliable(target, command, data, { targetIds?, seq? })

UDP when bound, automatic WebSocket fallback otherwise — browsers get WS delivery since they physically cannot send UDP.

heartbeat / close
rt.heartbeat()
rt.close()

Keepalive (auto-scheduled every 20s vs the 60s idle close) and clean shutdown — closes UDP socket and WS.

events
onConnected · onReceive · onSent · onError · onClose

Assign handlers via options or properties: onReceive(command, data, sender, seq) fires per peer message.

The two realtime lanes

Get mints the realtime credential for the player's current realtime-enabled room — one live token per player with roughly a five-minute connect window. Connect opens the WebSocket control lane (wss://): reliable, ordered messages addressed to all, host, others or a specific player list. The host ACL is enforced server-side — non-host clients may only address the host.

For twitch-rate traffic there is a second lane: Node.js clients can bind the MUR1 UDP fast lane, whose datagrams authenticate through the WebSocket-issued connection id. The unreliable send prefers UDP when bound and falls back to WebSocket automatically — browsers always take the WS path since they physically cannot send UDP.

  • Heartbeat is auto-scheduled around 20 seconds against a 60-second idle close; call close for a clean shutdown.
  • Attach event handlers before connecting so no early room events are missed.
  • Use reliable sends for state and actions; reserve the unreliable lane for high-frequency positions and motion.
  • Every call returns a Promise — await inside async functions. In browsers requests run over fetch with CORS; on Node.js 22.12+ the identical API uses the built-in fetch.