Messaging and events
All messaging is room-scoped: you must be in a room, and you can only reach players in the same room.
The three ways to send
| Action | Goes to | Arrives as |
|---|---|---|
sendToRoom | everyone in the room (not you, unless includeSelf) | event message |
sendToPlayer | one player in the room | event directMessage |
sendToMaster | the master | event message |
Data you send:
{ "action": "sendToRoom", "data": { "eventName": "chat", "data": { "text": "gg" }, "includeSelf": false } }
eventNameis required. It is your event name (chat,cast,scoreUpdated...).datais any JSON value you like.includeSelf(sendToRoom only) also delivers the message back to you.sendToPlayeralso needstargetPlayerId(the player'splayerId, not connection id). If they are not in your room you getPLAYER_NOT_FOUND.
How a message arrives
Riff wraps your payload. Receivers get:
{ "event": "message", "data": { "eventName": "chat", "data": { "text": "gg" }, "fromPlayerId": "p-123" }, "timestamp": 1760000000000 }
So there are two names: the outer event (message or directMessage, set by Riff) and your inner eventName. Your code switches on the inner one. fromPlayerId is set by the server and cannot be forged by the sender.
Events Riff sends on its own
| Event | When | data |
|---|---|---|
roomJoined | you joined | { room, players } |
roomLeft | you left | { roomId, playerId } |
roomList / roomState | replies to listRooms / getRoomState | see Lobby and rooms |
playerJoined | someone joined | { playerId, playerName, connectionId, joinedAt } |
playerLeft | someone left | { playerId, playerName } |
hostChanged | master changed | { newHostConnectionId } |
roomPropertiesChanged | master changed room props | { properties } (full merged set) |
playerPropertiesChanged | a player changed theirs | { playerId, playerName, properties } |
positionUpdate | see Gameplay | { playerId, x, y, z, rotation?, animState? } |
eventRaised | a raiseTurnEvent | { eventCode, data, fromPlayerId, fromPlayerName } |
turnChanged | master set the turn | { targetPlayerId } |
gameEnded | master ended the game | { results } |
pong | reply to ping | none |
error | something was refused | code, message (top level, not inside data) |
Every server message carries a timestamp (server milliseconds).
Errors
Errors arrive as { "event": "error", "code": "ROOM_FULL", "message": "..." }. The full list is in the reference. Refused actions are never silent, with one exception: the WebSocket handshake itself (see Connecting).
RATE_LIMITED means you exceeded 30 in-room messages in one second. Messages over the budget are dropped, the budget refills the next second, and the connection stays open. Batch high-frequency data (see position updates) instead of sending every frame.
Delivery guarantees
- Messages are delivered in the order Riff processes them, but there is no guaranteed ordering between different senders and no acknowledgements or retries. A player who has just dropped misses the message.
- There is no history: late joiners do not receive earlier messages. Put state they need in room properties or player properties, which are stored, and use messages for things that happen once.
Unity
await client.SendToRoomAsync("chat", new { text = "gg" });
await client.SendToPlayerAsync(targetPlayerId, "invite", new { room = "arena-1" });
await client.SendToMasterAsync("buyItem", new { itemId = 7 });
client.OnMessageReceived.AddListener(msg => {
// Outer event: "message" or "directMessage" (anything the SDK does not handle itself)
var name = msg.Data?["eventName"]?.ToString(); // your event name
var from = msg.Data?["fromPlayerId"]?.ToString();
var payload = msg.Data?["data"];
switch (name) { case "chat": /* ... */ break; }
});
The SDK's SendToRoomAsync sends without includeSelf; to echo to yourself, handle your own action locally.