12. Best Practices
1. Protocol Selection
| Use Case | Protocol | Reason |
|---|---|---|
| Position updates | McProtocol.Unreliable |
Fast, a newer update replaces a lost one |
| Player actions (shoot, jump) | McProtocol.Reliable |
Must arrive |
| Chat messages | McProtocol.Reliable |
Must arrive |
| Game state sync | McProtocol.Reliable |
Must arrive |
| Room join/leave | McProtocol.Reliable |
Critical operations |
2. Message Frequency
Script 12.1
// BAD: Send every frame
void Update()
{
room.SendMcBinary(position, GameMessage.Move, McProtocol.Unreliable);
}
// GOOD: Send at fixed intervals
private float sendInterval = 0.05f; // 20 times per second
private float lastSendTime = 0f;
void Update()
{
if (Time.time - lastSendTime >= sendInterval)
{
room.SendMcBinary(position, GameMessage.Move, McProtocol.Unreliable);
lastSendTime = Time.time;
}
}
3. Server-Side Validation
Script 12.2
// Registered with room.AddResponseListener(GameMessage.PlayerMove, OnPlayerMove)
void OnPlayerMove(string senderId, object obj)
{
var moveData = (PlayerMoveData)obj;
moveData.playerId = senderId; // Who really sent it, not who the message claims
// Always validate on server
if (!IsValidMove(moveData))
{
Debug.LogWarning($"Invalid move from {senderId}");
return;
}
// Then broadcast
room.SendMcBinaryToAll(moveData, GameMessage.PlayerMove, McProtocol.Unreliable);
}
4. Cleanup
Script 12.3
// Always clean up listeners
void OnDestroy()
{
room?.RemoveAllListener();
}
void OnApplicationQuit()
{
currentRoom?.LeaveRoom();
Client.DisconnectFromServer();
}
5. Security
Connect tokens for anything players can lose. Without them, a player is whoever their client says they are until that player is connected. For a game with accounts, coins or purchases, turn on Require Connect Token on the server and give every client a token from your own backend after it logs the player in. The server logs a warning at start while tokens are off.
Script 12.4
// Client, after your backend has logged the player in and returned a token for their id
Client.SetConnectToken(tokenFromYourBackend);
Your backend signs the token with the same secret the server has (Connect Token Secret, or MC_CONNECT_TOKEN_SECRET); ConnectIdentity.CreateSignedToken shows the format, and a backend in another language builds the same bytes.
Encryption. Raw TCP and UDP are not encrypted, and the connection's session secret travels on them. For anything sensitive use wss:// (MultiConnectCloud gives every Server deployment a wss:// address) and, in WebGL, the WebRTC channel for unreliable data. A client given both an address and a wss:// URL for a server on another machine uses wss://.
Entry fees and balances. A room's entry amount is checked against what the CLIENT reports unless your server checks it. Set MatchMaking.ValidateRoomEntry on the master to decide from your own records:
Script 12.5
MatchMaking.ValidateRoomEntry = (playerId, entryAmount) =>
PlayerWallets.TryGet(playerId, out var coins) && coins >= entryAmount;
Room limits. Seat counts and timings in a room request come from the client, so the server keeps them within McRoomRules (at most 128 seats, an empty room kept at most an hour, a disconnected player's seat held at most 10 minutes). Tighten them to what your game uses, or fix them outright:
Script 12.6
McRoomRules.FixedMaxPlayers = 2; // every room has two seats, whatever a client asks for
McRoomRules.MaxEmptyRoomSeconds = 120;

