# Replication Server API

This page illustrates a few API you can use to interact with the Replication Server.

## Connection to a Replication Server

When the Replication Server is running, you connect to it using the `Connect` method.

#### Connect to a local Replication Server

```csharp
using Coherence;
using Coherence.Connection;
using Coherence.Toolkit;
using UnityEngine;

public class ConnectToLocal : MonoBehaviour
{
    void Start()
    {
        var Bridge = FindAnyObjectByType<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

```csharp
using Coherence;
using Coherence.Connection;
using Coherence.Toolkit;
using UnityEngine;

public class ConnectToLocal : MonoBehaviour
{
    private CoherenceBridge Bridge;

    void Start()
    {
        Bridge = FindAnyObjectByType<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);
    }
}
```

{% hint style="info" %}
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.
{% endhint %}

{% hint style="info" %}
To connect to Cloud-hosted Servers, see [Rooms API](/1.4/hosting/coherence-cloud/coherence-cloud-apis/rooms-api.md) and [Worlds API](/1.4/hosting/coherence-cloud/coherence-cloud-apis/worlds-api.md) documentation.
{% endhint %}

{% hint style="info" %}
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.
{% endhint %}

To connect with multiple Clients locally, publish a build for your platform (*File > Build and Run*, details in [Unity docs](https://docs.unity3d.com/Manual/PublishingBuilds.html)). Run the Replication Server and launch the build any number of times. You can also enter Play Mode in the Unity Editor.

{% hint style="info" %}
For Mac Users: You can open new instances of an application from the Terminal:

<pre class="language-bash"><code class="lang-bash"><strong>open -n &#x3C;path to .app>
</strong></code></pre>

{% endhint %}

## Unlock token

By default, the number of players that can connect to a locally hosted Replication Server is limited to **100**.

{% hint style="info" %}
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](/1.4/hosting/client-hosting/implementing-client-hosting.md) scenario. Replication Servers running in the **coherence** Cloud have no player limit.
{% endhint %}

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](https://coherence.io).

<figure><img src="/files/xVXX0rQ1kpqXOAg27Aqq" alt=""><figcaption></figcaption></figure>

Once you have the token, it needs to be added to the **coherence** `RuntimeSettings` (*Assets/coherence/RuntimeSettings.asset*):

<figure><img src="/files/M1j8tgQPw6EzwZsS0kzd" alt=""><figcaption></figcaption></figure>

The unlock token will now be automatically passed to all the Replication Server instances started via Unity editor or the `Coherence.Toolkit.ReplicationServer` API.

{% hint style="info" %}
If you plan to execute the Replication Server manually the token can be supplied via the `--token <token>` command line argument.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherence.io/1.4/manual/replication-server/replication-server-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
