SDK 1.1
Ask or search…
K
Links

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 coherence 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. 1.
    Run the Replication Server by clicking the Run button or copy the run command to the clipboard via clicking the copy run-command icon located to the right of it.
  2. 2.
    A terminal/command line will pop up, running your Server locally.
  3. 3.
    The port the Replication Server will use. Rooms: 42001 Worlds: 32001
  4. 4.
    The web port used for webGL connections. Rooms: 42001 Worlds: 32002
  5. 5.
    The Replication Server send frequency. Default: 20 packets / s
  6. 6.
    The Replication Server receive frequency. Default: 60 packets / s
You can also start the Replication Server from the coherence menu or by pressing Ctrl+Shift+Alt+N.

Replication Server send and receive frequencies

The Replication Server supports different packet frequencies for sending and receiving data.
The send frequency is the frequency that the Replication Server uses to send packets to a given Client. Each Client can be sent packets at different times, but the packet receive frequency for any Client will not exceed the Replication Server's send frequency.
The receive frequency is the maximum frequency at which the Replication Server expects to receive packets from any Client, before throttling. If a Client sends packets to the Replication Server at a higher than expected frequency, that Client will receive a command to slow down sending. If the Client doesn't respect the command to throttle packet sending then the Client is disconnected after a time. All extra packets received by the Replication Server, after a threshold based on the receive frequency, are dropped and not processed. This is to prevent malicious Clients from flooding the Replication Server. The Unity SDK handles throttling automatically.
It is possible for the Replication Server to temporarily request Clients to reduce their packet send rates if the processing load of the Replication Server is too high. This is automatic and send rates from the affected Clients are commanded to resume once the load is reduced.

Adjusting send and receive frequencies

Low and consistent send rates from the Replication Server allow for optimal bandwidth use and still support a smooth stream of updates to Clients. Try different rates during local replication tests to see what works well for your game.
For a locally hosted Replication Server, you can edit the send and receive frequencies by using the CLI arguments --send-frequency and --recv-frequency. Or by changing it in the coherence Settings -> Local Replication Server -> Send Frequency / Recv Frequency.
On the dashboard, the packet frequencies for sending and receiving data can be adjusted per project too. It is part of the Advanced Config section of Worlds create/edit and Rooms pages of the dashboard.
Adjusting the send and receive frequencies on the dashboard is available for paid plans.

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

using Coherence;
using Coherence.Connection;
using Coherence.Toolkit;
using UnityEngine;
public class ConnectToLocal : MonoBehaviour
{
void Start()
{
var Bridge = FindObjectOfType<CoherenceBridge>();
var endpoint = new EndpointData
{
region = EndpointData.LocalRegion,
host = "127.0.0.1",
port = 32001,
schemaId = RuntimeSettings.instance.SchemaID,
};
Bridge.Connect(endpoint);
}
}
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

using Coherence;
using Coherence.Connection;
using Coherence.Toolkit;
using UnityEngine;
public class ConnectToLocal : MonoBehaviour
{
private CoherenceBridge Bridge;
void Start()
{
Bridge = FindObjectOfType<CoherenceBridge>();
Bridge.onConnected.AddListener(HandleConnected);
Bridge.onDisconnected.AddListener(HandleDisconnected);
Bridge.onLiveQuerySynced.AddListener(HandleLiveQuerySynced);
var endpoint = new EndpointData
{
region = EndpointData.LocalRegion,
host = "127.0.0.1",
port = 32001,
schemaId = RuntimeSettings.instance.SchemaID,
};
Bridge.Connect(endpoint);
}
private void HandleConnected(CoherenceBridge _) { /* ... */ }
private void HandleLiveQuerySynced(CoherenceBridge _) { /* ... */ }
private void HandleDisconnected(CoherenceBridge _, ConnectionCloseReason reason) { /* ... */ }
private void OnDestroy()
{
Bridge.onConnected.RemoveListener(HandleConnected);
Bridge.onDisconnected.RemoveListener(HandleDisconnected);
Bridge.onLiveQuerySynced.RemoveListener(HandleLiveQuerySynced);
}
}
The OnLiveQuerySynced event is triggered when the initial game state has been synced to the client. More specifically, it is fired when all entities found by the Client's first Live Query have finished replicating. This is the last step of the connection process and is usually a good place to start the game simulation.
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 even when they're not the active window.
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>

Unlock token

By default, the number of players that can connect to a locally hosted Replication Server is limited to 100.
By definition, a locally hosted Replication Server is one that is not managed by coherence, for example if it has been started from a Unity editor or by a game client in the self-hosting scenario. Replication Servers running in the coherence Cloud have no player limit.
This restriction can be lifted by supplying the SDK with an unlock token. The token can be generated in the Settings section of your project dashboard at coherence.io.
Once you have the token, it needs to be added to the coherence RuntimeSettings (Assets/coherence/RuntimeSettings.asset):
The unlock token will now be automatically passed to all the Replication Server instances started via Unity editor or the Coherence.Toolkit.ReplicationServer API.
If you plan to execute the Replication Server manually the token can be supplied via the --token <token> command line argument.