# CoherenceBridge

The Bridge establishes a connection between your scene and the coherence [Replication Server](https://docs.coherence.io/1.1/coherence-sdk-for-unity/replication-server). It makes sure all networked entities stay in sync.

When you place a GameObject in your scene, the Bridge detects it and makes sure all the synchronization can be done via the `CoherenceSync` component.

![coherence Bridge Inspector](https://content.gitbook.com/content/4xmLLczbgaGVeoF6vews/blobs/oxDq12ZkeI8fpto9kKt2/CoherenceBridge%20Inspector)

At runtime, you can inspect which Entites the Bridge is currently tracking.

![coherence Bridge Inspector at runtime](https://content.gitbook.com/content/4xmLLczbgaGVeoF6vews/blobs/6I4M7mWfG9fVrHGM67BG/Screenshot%202023-04-25%20154800.png)

A Bridge is associated with the scene it's instantiated on, and keeps track of Entities that are part of that scene. This also allows for multiple connections at the same time coming from the game or within the Unity Editor.

### Unity Events

The CoherenceBridge offers a couple of Unity Events in its inspector where you can hook your custom game logic:

#### OnLiveQuerySynced

This event is invoked when the Replication Server state has been fully synchronized, it is fired after OnConnected.

For example, if you connect to a ongoing game that has five players connected, when this event is fired all the entities and information of all the other players will already be synchronized and available to be polled.

#### OnConnected

This event is invoked the moment you stablish a connection with the Replication Server, but before any synchronization has happened.

Following the previous example, if you connect to an ongoing game that has five players connected, when this event is fired, you won't have any entities or information available about those five players.

#### OnDisconnected

This event is invoked when you disconnect from a Replication Server. In the parameters of the event you will be given a ConnectionCloseReason value that will explain why the disconnection happened.

#### OnConnectionError

This event is invoked when you attempt to connect to a Replication Server, but the connection fails, you will be returned a ConnectionException with information about the error.

### Client Connections

The Client Connections system allows you to keep track of how many users are connected and uniquely identify them, as well as easily send server-wide messages.

You can read more about the **Client Connections** system [here](https://docs.coherence.io/1.1/coherence-sdk-for-unity/client-messages).

### Connecting to Worlds/Rooms from coherence Cloud

If you have a [Developer Portal](https://docs.coherence.io/1.1/coherence-cloud/overview) account, you can connect to Worlds or Rooms hosted in coherence Cloud. You can use the **CloudService** instance from CoherenceBridge to fetch existing Worlds or create or fetch existing Rooms, after you fetch a valid World or Room, you can use the JoinWorld or JoinRoom methods to easily connect your client.

You can read more about the **Unity Cloud Service** [here](https://docs.coherence.io/1.1/coherence-cloud/using-coherence-cloud-in-unity).

### Persistent Entity limits

{% hint style="warning" %}
Currently, the maximum number of persistent Entities supported by the Replication Server is **32 000**. This limit will be increased in the near future.
{% 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.1/coherence-sdk-for-unity/components/coherencebridge.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.
