# Transport Samples

After managing your players with either a matchmaker or lobbies then creating a relay session for them using the Edgegap API, you will need to connect your players to the relay once it is ready. Edgegap offers a specific transport to do just that, and this guide will show you how to implement it in your project. The transport is available [here on our GitHub](https://github.com/edgegap/distributed-relay-examples/tree/main).

With Unity, there currently is a version of the transport for the following netcodes:

* Mirror;
* Fishnet;
* Unity Netcode for GameObjects (NGO).

### Adding the transport

After downloading the right transport for your netcode, you need to include it in your project. First, add it to your project files in the following location:

* **Mirror** : add the `Edgegap` folder under `Assets/Mirror/Transports`;
* **Fishnet** : add the `Edgegap KcpTransport` folder under `Assets/Fishnet/Plugins`;
* **NGO** : add the `EdgegapRelay` and `kcp2k` folders under `Assets/Edgegap`.

Then, add the `EdgegapKcpTransport` script to your `NetworkManager` gameObject, make sure to drag that new component in the `Transport` property of the gameObject as well.

{% hint style="info" %}
If the option is present, make sure to disable `Relay GUI` in the `EdgegapKcpTransport` component of the `NetworkManager`. Otherwise, it will interfere when attempting to connect to the relay.

This option is namely present in the Mirror and NGO versions of the transport.
{% endhint %}

### Connecting to the relay

After matchmaking and creating the relay session, use the Edgegap API to determine when the relay is ready to accept connections. Once it is, use the data provided in the API response to set the values of the transport. You will need the following values in the appropriate fields:

* the relay's `IP` address is used as the `transport's relay address`;
* the `session authorization token` is used as the `transport's session ID`;
* the `user's authorization token` is used as the `transport's user ID`.

#### Server/Host Connection

* the relay's `server port` value is used as the `transport's relay port` .

#### Client Connection

* the relay's `client port` value is used as the `transport's relay port` .

{% hint style="info" %}
Each player will have their own unique authorization token, but the session token will remain the same for each player in the session.
{% endhint %}

### Using Mirror

```cs
// `data` is the deserialized API response converted to JSON
// `_EdgegapTransport` is the EdgegapKcpTransport

// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;

//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;

_EdgegapTransport.ChangeValue(
    data.relay.ip,
    data.relay.ports.client.port,
    data.relay.ports.server.port,
    data.session_id,
    sessionAuthorizationToken,
    userAuthorizationToken
);

// then `NetworkManager.Singleton.StartHost();` if host player
// OR `NetworkManager.Singleton.StartClient();` if client 
```

### Using Fishnet

```cs
// `data` is the deserialized API response converted to JSON
// `_transport` is the EdgegapKcpTransport

// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;

//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;

Relay relay = data.relay;
string address = relay.ip;
ushort serverPort = relay.ports.server.port;
ushort clientPort = relay.ports.client.port;

var relayData = new EdgegapRelayData(
    address,
    serverPort,
    clientPort,
    userAuthorizationToken,
    sessionAuthorizationToken
);
_transport.SetEdgegapRelayData(relayData);

// then `_transport.StartConnection(true);` if host
// OR `_transport.StartConnection(false);` if client
```

### Using NGO

```cs
// `data` is the deserialized API response converted to JSON
// `_EdgegapTransport` is the EdgegapKcpTransport

// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;

//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;

_EdgegapTransport.relayAddress = data.relay.ip;
_EdgegapTransport.relayGameClientPort = data.relay.ports.client.port;
_EdgegapTransport.relayGameServerPort = data.relay.ports.server.port;
_EdgegapTransport.sessionId = sessionAuthorizationToken;
_EdgegapTransport.userId = userAuthorizationToken;

// then `NetworkManager.Singleton.StartHost();` if host player
// OR `NetworkManager.Singleton.StartClient();` if client 
```

### Using ApiResponse

```cs
public class ApiResponse
{
    public string session_id { get; set; }
    public uint? authorization_token { get; set; }
    public string status { get; set; }
    public bool ready { get; set; }
    public bool linked { get; set; }
    public object? error { get; set; }
    public List<SessionUser>? session_users { get; set; }
    public Relay relay { get; set; }
    public object? webhook_url { get; set; }
}
```

Once the values are properly set, use the transport to connect each player to the relay. After a short moment, you will be able to play the game!

### Sample projects

The following projects are simple examples that use the Edgegap relay transport.

For them to work properly, open a command prompt terminal and download the project via the `git clone [URL]` command. Open the project folder in the editor via the Unity Hub, then change the `RelayProfileToken` value in the `HelloWorldManager` component of the `NetworkManager` gameObject to your own relay profile token.

* [Mirror sample](https://github.com/edgegap/unity-mirror-relay-sample)
* [Fishnet sample](https://github.com/edgegap/unity-fishnet-relay-sample)
* [NGO sample](https://github.com/edgegap/unity-ngo-relay-sample)

{% hint style="info" %}
Use your custom [Relay Profile token](https://app.edgegap.com/relay-management/dashboard) in `HelloWorldManager.cs` or `EdgegapRelayService.cs`.
{% 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.edgegap.com/learn/distributed-relay/relay-transport-samples.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.
