Riff

Unity SDK

Package name com.aarware.relay. It uses Newtonsoft JSON (com.unity.nuget.newtonsoft-json) and includes a WebGL WebSocket plugin, so the same code runs in the editor, standalone builds, and WebGL.

Install

Add to Packages/manifest.json:

"com.aarware.relay": "<package URL from your dashboard>"

Unity pins the exact commit in Packages/packages-lock.json (hash). After the SDK changes, update that hash or Unity keeps the old code. Never edit files in Library/PackageCache.

Configure

Create a config asset: Assets → Create → Relay → Configuration.

FieldMeaning
serverUrlwss://ws.riffserver.com
appIdyour app's id
autoReconnectreconnect after an unexpected drop (default on)
reconnectMaxAttemptsattempts before giving up (default 5)
authTokenthe signed connect token, if the app requires one (set it before connecting)
debugModelog every message to the Console

Wire the config into your own component with [SerializeField] RelayConfig config; in the Inspector.

Start

public class Net : MonoBehaviour {
    [SerializeField] RelayConfig config;

    async void Start() {
        RelayManager.Instance.Initialize(config);
        var client = RelayManager.Instance.Client;
        client.OnMessageReceived.AddListener(OnMsg);
        await client.ConnectAsync(playerId: "player-123", playerName: "Ada");
        await client.JoinRoomAsync("lobby");
    }
}

RelayManager is a persistent singleton (survives scene loads). If you prefer, put RelayClient on your own GameObject and call Initialize(config) yourself. Every callback runs on the Unity main thread.

Actions

MethodNotes
ConnectAsync(playerId, playerName) / ConnectCoroutine(...)playerId defaults to a new GUID
Disconnect()stops auto-reconnect
JoinRoomAsync(name, RoomOptions)returns RoomState; times out after 5 s
LeaveRoomAsync(), ListRoomsAsync(), GetRoomStateAsync()wait for the reply (5 s timeout)
SetRoomPropertiesAsync, SetPlayerPropertiesAsyncfire and forget; confirmed via callbacks
SendToRoomAsync, SendToPlayerAsync, SendToMasterAsyncsee Messaging
UpdatePositionAsync(pos, rotation, animState)see Gameplay
RaiseEventAsync, SetTurnAsync, EndGameAsyncsee Gameplay

Calls made while disconnected are queued and sent when the connection opens.

State

PropertyMeaning
IsConnected, IsInRoomconnection and room state
LocalPlayerId, ConnectionIdwho you are
CurrentRoom, RoomPlayersthe room and its players
IsMasterClientare you the room master
Randomthe shared random generator

Callbacks (UnityEvents on RelayClient)

OnConnected, OnDisconnected(reason), OnReconnecting(attempt), OnPlayerJoined(Player), OnPlayerLeft(Player), OnRoomPropertiesChanged(dict), OnPlayerPropertiesChanged(...), OnMessageReceived(RelayMessage), OnPositionUpdate(PositionUpdate), OnTurnChanged(playerId), OnGameEnded(data), OnError(RelayError).

Anything the SDK does not handle itself (message, directMessage, eventRaised) arrives on OnMessageReceived.

Errors

Refused actions raise OnError with a code (ROOM_FULL, NOT_HOST, ...). The awaited calls (JoinRoomAsync and friends) throw a RelayError for the same codes, or a TimeoutException after 5 seconds.

WebGL

A browser-side plugin (RelayWebSocket.jslib) carries the socket. Only code inside its mergeInto({...}) block ships in the build, so state lives on Module. Do not add top-level variables there.

Conventions this SDK expects of your game

  • Wire references in the Inspector with [SerializeField]; the SDK never searches the scene for you.
  • Keep your own event names in one place (an enum or constants class) so sender and receiver agree.