Java SDK — Realtime

Realtime

Reliable WebSocket (TCP) plus an optional UDP fast lane — 1 TCP + 1 UDP per player

Realtime.getToken
public static TokenResponse getToken(
    Client client,
    String playerToken
) throws IOException

Retrieves a realtime authentication token for WebSocket connections.

Realtime.connect
public boolean connect(
    String realtimeToken
)

Connects to the realtime WebSocket server using the provided token.

Realtime.send
public void send(
    ERoomTargetPlayer target,
    String command,
    Object data,
    int[] targetIds
)

Sends a message to the specified players via WebSocket.

Realtime.connectUdp
public boolean connectUdp(
    Integer udpPort
)

Binds one UDP endpoint alongside the WebSocket (max 1 TCP + 1 UDP per player). Datagrams authenticate with the server-issued connection id. Null port = the udp_port announced by the server.

Realtime.sendUnreliable
public void sendUnreliable(
    ERoomTargetPlayer target,
    String command,
    Object data,
    int[] targetIds
)

Sends over the bound UDP channel — unordered, unacknowledged, latest-wins via seq. Ideal for 20-60Hz movement/state streams. Falls back to the WebSocket when UDP isn't bound.

Prediction helpers
Prediction.PredictionBuffer<S, I> — nextSeq() / record() / reconcile()
Prediction.SnapshotBuffer<S> — add(t, state, seq) / sample(renderTime, lerp)

Client-side movement prediction: record each input with its seq, adopt authoritative state on arrival, replay unacked inputs. SnapshotBuffer interpolates remote entities ~100ms in the past. In rooms/realtime/Prediction.java.

Realtime.disconnect
public void disconnect()

Disconnects from the server and cleans up both channels.

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.
  • Calls return CompletableFuture — compose with thenApply/thenAccept, or join() on a worker thread when blocking is acceptable; never join on a UI or render thread.