# 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.

![](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MYvnBmCiZcQMqentOtI%2F-MYvnJJ28A_KmVzZc-8_%2Fimage.png?alt=media\&token=e3d502a5-65cc-44e4-addf-02cdef63a201)

1. Run the Replication Server by clicking the run button.
2. A terminal/command line will pop up running your server locally&#x20;
3. The port the Replication Server will use. Default: `32001`.
4. The Replication Server frequency. Default: `60`.

{% hint style="info" %}
You can also start the replication server from the **coherence** menu or by pressing  ctrl+shift+alt+N.
{% endhint %}

{% hint style="warning" %}
If you're unsure where schema files are located, you can easily search through the project using Unity's project search window, with`t:Coherence.SchemaAsset` &#x20;
{% 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 %}

## 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

```csharp
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

```csharp
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;
}

```

{% hint style="info" %}
To connect to cloud hosted servers, see [Rooms API](https://docs.coherence.io/0.7.4/network-sdk/rooms#rooms-api) and [Worlds API](https://docs.coherence.io/0.7.4/network-sdk/worlds#worlds-api) documentation.
{% endhint %}

{% hint style="info" %}
Check 'Run in Background' in the Unity settings under Project Settings -> Player so that the clients continue to run when not the active window.&#x20;
{% endhint %}
