# Azure PlayFab Relay

{% hint style="success" %}
Before you start setting up PlayFab, consider that the [coherence Cloud](https://docs.coherence.io/hosting/coherence-cloud) is:

* [Free to start](https://coherence.io/pricing)
* Does not require the time-consuming process of setting up a relay
  {% endhint %}

[PlayFab](https://playfab.com) is a backend platform for building and operating live games. It provides a suite of managed game services, real-time analytics, and LiveOps tools within a single platform. It provides a free service as well as pay-per-month plans. In order to use PlayFab you'll need to set up a game with a valid **PlayFab Title ID**. Once you have a PlayFab Title ID, you'll be able to pass messages between clients via PlayFab's servers.

{% hint style="info" %}
You need a credit card to activate the multiplayer services in PlayFab.
{% endhint %}

To make things easy, **coherence** provides a complete PlayFab relay implementation that provides out-of-the-box networking over PlayFab. The PlayFab relay utilizes the [PlayFabPartyUnity package](https://github.com/PlayFab/PlayFabPartyUnity) to access the **PlayFab Party API**.

The **PlayFab Integration** is using a combination of the PlayFab relay servers and an implementation of `ICoherenceRelay` to enable PlayFab users to connect and play with each other while avoiding NAT issues. The `ICoherenceRelay` implementation allows users to connect to the hosting client through PlayFab, and have the client forward their data packets to the user-hosted Replication Server.

The sample code in the repository demonstrates how to **join and host a game** with the **PlayFab Party API**.

{% hint style="info" %}
The Azure PlayFab relay is available here: <https://github.com/coherence/playfab-integration-sample>
{% endhint %}

<figure><img src="https://content.gitbook.com/content/CMCtKgV0bk1lwR4tWK3W/blobs/4313qrAUMptsb5tnuT02/playfab-diagram.png" alt=""><figcaption><p>PlayFab relay overview</p></figcaption></figure>

### **Connecting over PlayFab, step-by-step**

1. The host (Client A) initializes the PlayFabClientAPI that handles PlayFab connections and then starts a Replication Server on its local machine.
2. The host connects to the local Replication Server and joins the PlayFab network. The host receives the remote server's **network ID** and **host id**. These are used by clients to connect to PlayFab's services.
3. Another player (Client B) connects to the host via PlayFab using the `PlayFabTransport`.
4. The `PlayFabRelay` accepts the incoming connection, creating a `PlayFabRelayConnection`.
5. The `PlayFabRelayConnection` immediately starts passing data between the PlayFab servers and the Replication Server.
6. The relayed connection is now fully established. All data between Client B and the Replication Server is relayed through PlayFab.
7. For each new Client that connects, steps 3-6 are repeated.

{% hint style="success" %}
Although the diagram above shows that traffic is routed via PlayFab servers, it is often the case that traffic can flow directly between player and host machines without actually making the extra hop via the PlayFab servers.

This technique is commonly referred to as "hole punching" or "**NAT Punch-through**" and greatly reduces latency. However, it is not supported on all networks due to firewall restrictions.

PlayFab Party will first attempt a NAT punch-through and then automatically fall back to relayed communication if the punch-through failed.
{% endhint %}
