Add Relays to Peer-To-Peer Networking Games in Unity - Video Tutorial (Mirror Networking Netcode)

Please note this specific video uses Mirror Networking as an example. NGO and Fish-Net videos should be available soon, however beyond the folder and location of certain files, nothing in the process changes.

Chapter 1: Introduction  


In this video, we will cover how to add, and set up, Edgegap’s distributed relays for peer-to-peer networking to your multiplayer game in just a few minutes.

Relays serve as an intermediate connection between players without having to use a dedicated server. Additionally, relays help bypass restrictions related to players direct connection, such as strict firewall or NAT settings, and improve performance by handling network traffic at the same time as running the game. For more information, check out our blog linked in the description.

Let’s get right to it!

Chapter 2: Setup

Before implementing relays in your game, you first need a token which let’s the game interact with Edgegap’s relay session API.

To get one, either create an Edgegap account or log directly to your account.

From the dashboard, navigate to the Relay page using the left sidebar. Scroll down to the “Relay Profiles” section. Here, click on “Create Relay Profile.” Give your token a nickname, then click on “Submit.” Your relay API token is now visible.

This token is used in your project to manage your API requests; namely create new relay sessions, get information on existing ones, and terminate them.

While optional during development, we strongly recommend combining relays to lobbies. As it groups players and create a session for them. The benefits are numerous, including improving privacy such as shielding player’s IP, automation and preventing cheaters and hackers, and more. Our simple guide on lobbies is linked.

We will use a manual connection in this tutorial, yet we will highlight where the lobbies queries would be in the code.

Whichever grouping method is used, once players are selected to be in the relay’s session, it requires a connection. This transport can be any netcode solution, such as Mirror Networking, Fish-Networking, or Unity Netcode for GameObjects. Edgegap documentation, alongside our sample on GitHub, covers each application for these netcodes.

Chapter 3: Network Manager

Now, on to the actual work!

First, we are going to add Edgegap sample to your game’s project.

From your file manager, head over to the Edgegap sample folder, then go into the subfolder where three sample netcode are available. Select your netcode, in this specific example, we select “Mirror”.

Copy the folder called “Edgegap”.

Switch to your project’s folder. Select “Asset”, then your netcode which, again, is “Mirror”. Then click on “Transport”, and now paste the Edgegap sample project’s folder.

Now, open your project in Unity.

The first step is to swap your transport component with the new one from the sample. To do so, from the editor, first select “Network Manager”.

Then, remove your current Transport Component, in this example named “KCP Transport”.

Secondly, open Edgegap’s sample in the assets. The path in our example starts by selecting the project, here named “Mirror”, then click on “transport”, select “Edgegap”, followed by “Edgegap Relay”. The file is named “Edgegap KCP Transport dot CS”. Drap and drop it to the Network Manager in the editor.

The third step is to open the Network Manager again, then select “Transport” as to add the new Edgegap KCP transport.

Finally, make sure to disable the “Relay GUI” in the transport’s inspector.

Chapter 4: Script - Send Requests to Edgegap API

As we lack a lobby system, the next step is to create a script to send requests to the Edgegap API. Three functions must be added.

The first is to create a new relay session, the second to get data on a specific relay session, and the third is to have other players join and existing relay session. Each request needs to include the “Relay Profile API” token in the authentication headers, using “Token” as its “scheme” and the token we generated from Edgegap as its “value”. 

To create a new relay session, include a list with the individual IP address of every player in the group in the body of the request. If you use a lobby, it will come from that component, but given our example is for a manual grouping of players, we can use the same IP for each of them for testing purposes. To make this example as simple as possible, let’s select only two players. Once the session is created, its unique ID will be included in the API response.

We use the unique session ID from the response to send a “GET request” for that specific relay session’s information, which uses the ID as a path parameter. This function will keep sending the same request on a loop until the response indicates that the relay is ready to accept connections.

Once the relay is ready, we return the latest response content to use for later.

We use our third function to let the other player join the relay, whose ID we already know. It also sends a GET request with the unique relay session ID to get information but skips the verification since we manually call this function when we already know that the relay is ready. We return the response content to use for later.

These functions let us manually test the relay without having to use a lobby. Normally, both the host and the other players would wait for the relay to be ready at the same time, then join all at once when the data becomes available.

From there, create a new script to orchestrate this process with the player’s inputs. In our example, we create a basic UI with an input field and two buttons and attach the script to a game object.

The “Create” button calls our create request function, while the “Join” button uses the value of the input field as the ID of the session to join and call the join request function.

Once the data is returned, use it to set the values of the transport. The required values are the “Session Authorization token”, which is shared among every player in the session, the “User Authorization token” which is unique for each player, the relay’s “IP address,” the “Server Port” that is used by the player hosting the match which requested the session’s creation, and the “Client Port” that is used by the other players.

In this example, the host player’s unique token is the first in the response’s list, while the joining player’s token is the second.

Once the values are set, use the appropriate transport function to initiate the connection, depending on if the player is the host or not. From there, the players can now play your game!

There is one more thing to do, though. You must include a system to send an API request to terminate the relay session once the match has ended. This ends the players’ connection to the relay. 

Chapter 5: Conclusion  

That is all for this tutorial on adding relays to your game!

The next step in your development is likely to go beyond peer-to-peer with authoritative servers. Make sure to check out our video tutorial for Unity.

For questions and help, you’re welcome to join Edgegap’s Discord community.

Thanks for watching!