C++ SDK — Realtime

Realtime

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

Constructor
Realtime(
    const std::string& realtimeWebSocketUrl = "wss://realtime.michitai.com"
)

Initializes the realtime WebSocket client with optional custom server URL.

getToken (Static)
static TokenResponse getToken(
    Client& client,
    const std::string& playerToken
)

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

buildWebSocketUrl (Static)
static std::string buildWebSocketUrl(
    const RealtimeServerInfo& serverInfo,
    const std::string& token,
    const std::string& clientType = "json"
)

Constructs a WebSocket URL from server info and token.

connect
bool connect(const std::string& realtimeToken)

Connects to the WebSocket server using the realtime token. Returns true if successful, false if failed. Requires WebSocket library implementation (e.g., websocketpp, uWebSockets).

send
void send(
    RoomTargetPlayer target,
    const std::string& command,
    const nlohmann::json& data = nlohmann::json::object(),
    const std::vector<int>& targetIds = {}
)

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

connectUdp
bool connectUdp(int udpPort = 0)

Binds one UDP endpoint alongside the WebSocket (max 1 TCP + 1 UDP per player). Datagrams authenticate with the server-issued connection id. 0 = the udp_port announced by the server. Uses native sockets — links ws2_32 on Windows.

sendUnreliable
bool sendUnreliable(
    RoomTargetPlayer target,
    const std::string& command,
    const nlohmann::json& data = nlohmann::json::object(),
    const std::vector<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
PredictionBuffer<TState, TInput> — nextSeq() / record() / reconcile()
SnapshotBuffer<TState> — 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. Header-only in Realtime/Prediction.h.

disconnect
void disconnect()

Disconnects from the server and cleans up both channels.

setReceiveCallback
void setReceiveCallback(ReceiveCallback callback)

Sets the callback function to be invoked when a message is received from the server.

setConnectedCallback
void setConnectedCallback(ConnectedCallback callback)

Sets the callback function to be invoked when the WebSocket connection is established.