Replication Server

The Replication Server replicates the state of the world to all connected Clients and Simulators.

To understand what is happening in the game world, and to be able to contribute your simulated values, you need to connect to a Replication Server. The Replication Server acts as a central place where data is received from and distributed to interested clients.

You can connect to a Replication Server in the cloud, but we recommend that you first start one locally on your computer. coherence is designed so you can easily develop everything locally first before deploying to the cloud.

Replication Servers replicate data defined in schema files. The schema's inspector provides all the tools needed to start a Replication Server.

  1. Run the Replication Server by clicking the run button.

  2. A terminal/command line will pop up running your server locally

  3. The port the Replication Server will use. Default: 32001.

  4. The Replication Server frequency. Default: 60.

You can also start the replication server from the coherence menu or by pressing ctrl+shift+alt+N.

If you're unsure where schema files are located, you can easily search through the project using Unity's project search window, witht:Coherence.SchemaAsset

To connect with multiple clients locally, publish a build for your platform (File > Build and Run, details in Unity docs). Run the Replication Server and launch the build any number of times. You can also enter Play Mode in the Unity Editor.

For Mac Users: You can open new instances of an application from the Terminal:

open -n <path to .app>

Connecting to a Replication Server

When the replication server is running, you connect to it using the Connect method.

Connect to a local Replication Server

Coherence.Network.Connect("127.0.0.1:32001");

After trying to connect you might be interested in knowing whether the connection succeeded. The Connect call will run asynchronously and take around 100 ms to finish, or longer if you connect to a remote server.

Respond to connection events

private void Start() 
{ 
    Coherence.Network.OnConnected += HandleConnected;
    Coherence.Network.OnDisconnected += HandleDisconnected;    
    Coherence.Network.Connect("127.0.0.1:32001");
}

private void HandleConnected() { /* ... */ }

private void HandleDisconnected() { /* ... */ }

private void OnDestroy()
{
    Coherence.Network.OnConnected -= HandleConnected;
    Coherence.Network.OnDisconnected -= HandleDisconnected;
}

To connect to cloud hosted servers, see Rooms API and Worlds API documentation.

Check 'Run in Background' in the Unity settings under Project Settings -> Player so that the clients continue to run when not the active window.

Last updated