.NET (C#) SDK — Realtime

Realtime

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

Constructor
public Realtime(string realtimeWebSocketUrl = "wss://realtime.michitai.com")

Initializes the realtime WebSocket client with optional custom server URL.

GetTokenAsync (Static)
public static async Task<TokenResponse> GetTokenAsync(
    Client client, 
    string playerToken
)

Static method to generate a realtime authentication token using the API client.

ConnectAsync
public async Task<bool> ConnectAsync(string realtimeToken)

Connects to the WebSocket server using the realtime token. Returns true if successful, false if failed.

SendAsync
public async Task SendAsync<T>(
    ERoomTargetPlayer target,
    string command,
    T? data = null,
    int[]? targetIds = null
)

Sends a message to specific targets (host, all, others, or specific player IDs) with optional data.

ConnectUdpAsync
public async Task<bool> ConnectUdpAsync(int? udpPort = null)

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.

SendUnreliableAsync
public async Task SendUnreliableAsync(
    ERoomTargetPlayer target,
    string command,
    object? data = null,
    int[]? targetIds = null
)

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
PredictionBuffer<TState, TInput> — NextSeq() / Record() / Reconcile()
SnapshotBuffer<TState> — Add(t, state, seq) / TrySample(renderTime, lerp, out state)

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 Multiplayer/Rooms/Realtime/Prediction.cs.

DisconnectAsync
public async Task DisconnectAsync()

Disconnects from the server and cleans up both channels.

Events
public event Action<string, object, SenderInfo>? OnReceive;
public event Action? OnConnected;

OnReceive: Fired when a message is received from the server. OnConnected: Fired when WebSocket connection is established.

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.
  • Methods are Task-based async — await them and pass a CancellationToken tied to your game's lifetime so in-flight requests cancel cleanly on shutdown.