Client connections

Overview

The Client connection system lets you uniquely identify users connected to the same session, find any user by their ID, spawn objects whenever a new user joins the session, and send messages between those users.

To achieve this a special connection entity is automatically created by the Replication Server for each connected Client, including a Simulator. Those Entities are subject to a different rule set than standard Entities. Connection entities:

  • Can't be created or destroyed by the Client - they are always Replication Server-driven

  • Are global - they are replicated across Clients regardless of the in-simulation distance or LiveQuery extent

Client connections shine whenever there's a need to communicate something to all the connected players. Usage examples:

  • Global chat

  • Game state changes: game started, game ended, map changed

  • Server announcements

  • Server-wide leaderboard

  • Server-wide events

Enabling client connections

The global nature of Client connection doesn't fit all game types - for example, it rarely makes sense to keep every Client informed about the presence of all players on the server in an MMORPG (think World Of Warcraft). If this is your use case, make sure Client connections are turned off by default.

To enable Client connections, check that Global Query in the MonoBridge is turned on (it should be by default):

MonoBridge component

Disabling Global Query on one Client doesn't affect other Clients, i.e. the connection Entity of this Client will still be visible to other Clients that have the Global Query turned on.

Connection management

Most of the Client connection functionality is accessible through the CoherenceMonoBridge.ClientConnections object:

Each connection is represented by a plain C# CoherenceClientConnection object. It contains all the important information about a connection - its ClientID, Type, whether it IsMyConnection, and a reference to the GameObject and Coherence Sync associated with it.

Connection objects

Each Client connection can have a GameObject with CoherenceSync automatically being spawned and associated with it. Those objects, like any other objects with CoherenceSync, can be used for syncing properties or sending messages, with a little twist - they are global and thus not limited by the LiveQuery extent. That makes them perfect candidates for operations like:

  • Syncing global information - name, stats, tags, etc.

  • Sending global messages - chat, server interaction

To enable connection objects:

1. Create a client connection prefab

This step is described in detail in the Prefab setup section. In short, a Prefab with a CoherenceSync and a custom component (PlayerConnection in this example) must be created and placed in a Resources folder:

PlayerConnection prefab

For the system to know which object to create for every new Client connection, we have to link our Prefab to the MonoBridge. Simply drag the prefab to the Client field in the MonoBridge inspector:

MonoBridge with linked connection prefab

From now on every new connection will be assigned an instance of this Prefab, which can be accessed through the CoherenceClientConnection.GameObject property.

Prefab selection

Note that there's a separate field for the Simulator Connection Prefab. It can be used to spawn a completely different object for the Simulator connection that may contain Simulator-specific commands and replicated properties. If the field is left empty, no object will be created for the Simulator connection.

The Prefab selection process can be also controlled from code using the CoherenceMonoBridge.ClientConnections.ProvidePrefab callback:

A Prefab provided through the ProvidePrefab callback takes precedence over Prefabs linked in the inspector.

Client messages

Client messages are commands sent between the Client connection objects. Implementing Client messages is as simple as adding a new method to the component used by our connection Prefab and binding it in the configuration:

Don't forget to bind the new command:

Binding a client message

Client messages can be sent using the CoherenceClientConnection.SendClientMessage method:

If the ClientID of the message recipient is known we can use the CoherenceMonoBridge.ClientConnections directly to send a client message:

Last updated

Was this helpful?