# 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

Before you connect, make sure you've [baked](https://docs.coherence.io/manual/baking-and-code-generation). Then, (re)start a Replication Server for Worlds or Rooms. In this example, we're going for Worlds.

<figure><img src="https://1153445090-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCMCtKgV0bk1lwR4tWK3W%2Fuploads%2FTUNvhXhRqQpFGV1jA6AT%2Fimage.png?alt=media&#x26;token=ef6f09a3-b02f-4d18-9cd3-bd443c6072db" alt=""><figcaption></figcaption></figure>

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

public class ConnectToLocal : MonoBehaviour
{
    private CoherenceBridge bridge;

    private void Start()
    {
        if (!CoherenceBridgeStore.TryGetBridge(out bridge))
        {
            bridge = new GameObject(nameof(CoherenceBridge)).AddComponent<CoherenceBridge>();
        }

        bridge.onConnected.AddListener(HandleConnected);
        bridge.onDisconnected.AddListener(HandleDisconnected);
        bridge.onLiveQuerySynced.AddListener(HandleLiveQuerySynced);

        bridge.Connect();
    }
}

```

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.Connection;
using Coherence.Toolkit;
using UnityEngine;

public class ConnectToLocal : MonoBehaviour
{
    private CoherenceBridge bridge;

    private void Start()
    {
        if (!CoherenceBridgeStore.TryGetBridge(out bridge))
        {
            bridge = new GameObject(nameof(CoherenceBridge)).AddComponent<CoherenceBridge>();
        }

        bridge.onConnected.AddListener(HandleConnected);
        bridge.onDisconnected.AddListener(HandleDisconnected);
        bridge.onLiveQuerySynced.AddListener(HandleLiveQuerySynced);

        bridge.Connect();
    }

    private static void HandleConnected(CoherenceBridge bridge) { /* ... */ }

    private static void HandleLiveQuerySynced(CoherenceBridge bridge) { /* ... */ }

    private static void HandleDisconnected(CoherenceBridge bridge, 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](https://docs.coherence.io/hosting/coherence-cloud/coherence-cloud-apis/rooms) and [Worlds API](https://docs.coherence.io/hosting/coherence-cloud/coherence-cloud-apis/worlds) 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:

```bash
open -n <path to .app>
```

{% endhint %}
