Lightnet Small Network Relay
Explore how to run your own small network relay on Edgegap, with a Unity game sample that plays through it using Mirror, FishNet, PurrNet or Netcode for GameObjects (NGO).
The relay is a small .NET server built on LightNet. Edgegap hosts and orchestrates it like any other server: one deployment, started near your players. One player hosts a room and gets a 6-letter code, the other players join with that code. No player opens a port: everyone, including the host, is a client of the relay.
This sample shows:
a relay server in a
linux/amd64Docker image, listening on port7777for both TCP (reliable messages) and UDP (unreliable messages),rooms with a join code - many rooms can share one relay deployment,
the Tanks sample on four netcodes - Mirror, FishNet, PurrNet and NGO in one Unity project, one build,
one setting to switch netcode - in a settings asset, an editor menu, in the game before connecting, or on the command line,
optional Edgegap matchmaking - off by default: Find Match puts nearby players together on a relay near them, one player per room hosts it, players who search later join running rooms, and a relay with full rooms opens new ones before another relay starts,
optional encrypted connection -
wss://through Edgegap's TLS Upgrade, next to plain TCP and UDP,automated tests - game clients play through the relay on each netcode, with room codes or through the matchmaker.
โ๏ธ Preparation
Before we start, you will need:
Unity
6000.0.54f1, to open and build the game,.NET 8 SDK (optional), only to run the relay outside Docker.
The repository has four parts:
LightNet
LightNet networking core as a Unity package, plus the relay protocol and its netcode-agnostic client.
relay-server
Relay Server - .NET 8 console app built from the same sources, and its Dockerfile.
tanks
the Unity project: Mirror's Tanks example on Mirror, FishNet, PurrNet and NGO.
matchmaker-config.json
optional matchmaker configuration, see Optional Matchmaking.
๐ Pick the Netcode
The netcode is the netcode field of tanks/Assets/Tanks/Resources/TanksSettings.asset: Mirror, FishNet, PurrNet or NGO. The same asset holds the relay address and port.
You can also switch it:
in the Unity Editor, with the Tanks > Netcode menu - this also opens that netcode's scene,
in the game, with the netcode buttons at the top of the screen, before hosting or joining,
on the command line, with
-netcode FishNet.
Mirror
66.0.9
SyncVar, Command, ClientRpc, client authoritative NetworkTransform
FishNet
4.7.4
SyncVar<int>, ServerRpc, ObserversRpc, client authoritative NetworkTransform
PurrNet
1.24.1
SyncVar<int>, ServerRpc, ObserversRpc, owner authoritative NetworkTransform
NGO
1.13.0
NetworkVariable<int>, ServerRpc, ClientRpc, owner authoritative NetworkTransform
Each netcode has its own relay transport, built on the same relay client (RelayPeer): MirrorRelayTransport, FishNetRelayTransport, PurrNetRelayTransport and NgoRelayTransport. To use the relay in your own game, copy the LightNet package and the transport for your netcode.
โก Deploy and Connect
1. Run the Relay Locally
โ๏ธ From the repository root, build and run the relay image:
โ๏ธ Open tanks in Unity, pick a netcode, and build with Tanks > Build Windows client, or press Play on Assets/Tanks/Scenes/Boot.unity.
โ๏ธ In the first game, press Host: the room code appears on screen. In the second game, type the code and press Join room.
โ You may now proceed to the next step.
2. Push the Relay Image
โ๏ธ To get started, you'll need to create a free account with Edgegap. No credit card required.
โ๏ธ Find your Container Registry project name and credentials.
โ๏ธ Sign in to Edgegap's Container Registry with Docker, using your registry project name, username and token:
โ๏ธ Tag and push the relay image, with a new tag for every build:
โ You may now proceed to the next step.
3. Create an App Version
โ๏ธ Create an application named lightnet-relay, and a version with:
image
registry.edgegap.com/<project>/lightnet-relayand your tag,your Container Registry username and token, so Edgegap can pull your image,
resources 0.25 vCPU and 512 MB memory,
one port
7777, namedrelay, protocol TCP/UDP,optional, for the encrypted connection: a second port
7778, namedrelay-ws, protocol WS, with TLS Upgrade on,max duration 30 minutes for testing.
Optional relay settings, as environment variables on the app version:
RELAY_TCP_PORT
7777
port for reliable messages
RELAY_UDP_PORT
7777
port for unreliable messages
RELAY_MAX_PEERS
256
maximum concurrent connections, across all rooms
RELAY_IDLE_EXIT_SECONDS
600
stop the relay when no room exists for this long, 0 = never (relays started by the matchmaker stop when their last room ends)
RELAY_WS_PORT
7778
port for the WebSocket connection, 0 = off
RELAY_MATCH_MAX_PLAYERS
4
players per room on relays started by the matchmaker; backfill fills up to this
RELAY_MAX_ROOMS
4
rooms per relay started by the matchmaker
RELAY_SOLO_ROOM_SECONDS
120
close a room whose host has been alone this long; the game then searches again
MATCHMAKER_TOKEN
matchmaker Auth Token, for backfill
โ You may now proceed to the next step.
4. Deploy and Play
โ๏ธ Deploy your new app version near your players. When it is ready, note its host name (<id>.pr.edgegap.net) and the external port of relay.
โ๏ธ In both games, set the relay address and port on screen before connecting, or start them from the command line (alternative):
โ๏ธ Host a room in the first game and join it with the code from the second.

โ๏ธ Once you verify youโre able to connect to your Deployment without issues and are done testing, Stop your Deployment to free up capacity in your account for the next build.
In case you encounter issues, inspect Dashboard logs of your deployment.
If you canโt figure out the issue, weโre hanging out in our Community Discord and happy to help.
๐ Congratulations on your first Deployment on Edgegap! If youโd like to learn more, keep reading.
๐ฏ Optional Matchmaking
Room codes work among friends. To match players who don't know each other, turn on Edgegap Matchmaking: players press Find Match, the matchmaker groups 2 to 4 players near each other and starts a relay for their match, near them. The relay then picks one player to host and gives the others the room code, so nobody types a code. Players who press Find Match later join rooms with free seats, and a relay whose rooms are full opens new ones, before the matchmaker starts another relay. It's off by default, and hosting and joining by code keep working next to it.
How a match starts:
Each player's game measures its latency to Edgegap's beacons and creates a matchmaking ticket.
The matchmaker groups the tickets and deploys a relay from your app version. Edgegap gives that relay the list of matched tickets (
MM_TICKET_IDS).Each game connects to the relay with its ticket ID. The first matched player to ask hosts the match room, the others join it. Tickets that are not in the match are refused.
While a room has free seats, the relay asks the matchmaker for more players with backfills. Once every room is full, it offers a new room instead. See step 3 below.
When the last room ends, the relay deletes its backfills and stops its own deployment, so an ended match costs nothing.
A relay deployed without a matchmaker works as before, with room codes.
1. Create a Matchmaker
โ๏ธ Open matchmaker-config.json from the repository and set application.name and version to your app version from step 3:
For its first 10 seconds a ticket can only join a relay that has room (min_team_size 9999 can't be met). After 10 seconds the usual rule applies, and 2 to 4 waiting players get a new relay.
The beacons rule keeps players with players near them: a beacon counts if the player's latency to it is under 150 ms, and matched players' latencies to it must be within 100 ms of each other. The game sends its latencies in every ticket (Matchmaker Latency Beacons in TanksSettings.asset, on by default).
โ๏ธ In the dashboard, open Matchmaker, click Create Matchmaker, upload the file, then Create and Start.
โ๏ธ When the matchmaker is Online, copy its API URL and Auth Token.
โ๏ธ Add them to your app version as the environment variables MATCHMAKER_URL and MATCHMAKER_TOKEN, so relays can fill free seats (step 3).
โ You may now proceed to the next step.
2. Find a Match
โ๏ธ In tanks/Assets/Tanks/Resources/TanksSettings.asset, turn on Use Matchmaker and paste the Matchmaker Url and Matchmaker Token. The profile (casual) and port name (relay) already match the configuration above.
โ๏ธ Build the game, start two copies and press Find Match in both. Or start them from the command line:
โ๏ธ Both games show the match status, then one hosts and the other joins: both tanks appear in both games.
The matchmaking client is Assets/Tanks/Core/TanksMatchmaker.cs, built on the Edgegap Unity SDK. It never depends on the netcode: it puts the relay address in the settings and starts the game as host or as player, like the Host and Join room buttons do.
โ You may now proceed to the next step.
3. One Relay, Several Rooms
A relay started by the matchmaker keeps working for it after its first match starts, through backfills. A backfill is a ticket owned by the relay that carries its own address. When the matchmaker puts a player's ticket in one, that player gets the relay's address, asks for its role like the others and joins or hosts a room. The game needs nothing extra: it's the same Find Match flow.
Rooms with free seats come first. While a room has fewer than
RELAY_MATCH_MAX_PLAYERS(default4) players, in the room or on their way, the relay keeps up to 3 backfills open for it. A player placed there joins that room.Then new rooms. Once every room is full and the relay has fewer than
RELAY_MAX_ROOMS(default4) rooms, it offers one new room. The player placed there hosts it, and the room fills like the others.Only nearby players. The relay sends the room's own players' tickets with its backfills, so the
beaconsrule checks a new player against them.A host left alone for
RELAY_SOLO_ROOM_SECONDS(default120) is told the room closed, and the game searches for another match by itself.The relay stops when its last room ends, not when one host leaves. It deletes its backfills first, so no player is sent to a relay that is gone.
Rate limits. At most 5 backfills are open per relay, created at least 0.3 seconds apart.
โ๏ธ With a match running between two games, press Find Match in a third: it joins the running room, and all three games see three tanks.
โ๏ธ Fill a room with 4 games, then press Find Match in two more: the first hosts a new room on the same relay, and the second joins it.
The relay's backfill code is in relay-server/MatchBackfill.cs and relay-server/RelayServer.cs.
๐ Encrypted Connection
By default, game traffic through the relay is not encrypted: it goes over plain TCP and UDP. What protects players is that nobody sees another player's IP address, and with the matchmaker only matched players get the room code. Calls to the matchmaker use HTTPS.
For encryption, players can connect over a WebSocket instead. With TLS Upgrade on the relay-ws port (step 3), Edgegap holds the certificate and the connection is wss://: traffic between each player and Edgegap is encrypted, and the relay needs no certificate.
โ๏ธ Give the game a wss:// relay address instead of a host name, with the external port of relay-ws: on screen, in TanksSettings.asset, or on the command line:
โ๏ธ With the matchmaker, turn on Matchmaker Use Web Socket in TanksSettings.asset (or -matchmakerWebSocket): the game connects to the match relay's relay-ws port.
๐งช Automated Test
With a Windows build and a running relay (local or on Edgegap), e2e.sh starts a host and a client on each netcode:
The client drives and fires. A netcode passes when both games see two tanks, both received data on the reliable and unreliable channels, both saw a projectile spawn, and the host saw the client's tank move.
With a matchmaker, e2e-matchmaker.sh runs the same test through it: on each netcode, two games press Find Match and play the match on the relay the matchmaker started. With BACKFILL=1, a third game presses Find Match once the match runs and must join it through backfill. With MULTIROOM=1, four games fill a room, then a fifth must host a new room on the same relay and a sixth must join it. With MM_WEBSOCKET=1, the games connect over wss://.
๐ก Good to Know
How the relay forwards data - the host's messages go to one player, and each player's messages go only to the host. Players never talk to each other directly. Reliable messages go over TCP and unreliable ones over UDP, or both over one WebSocket.
Message size - up to 1,177 bytes per unreliable message and 16 KB per reliable message. The transports report these limits to their netcode.
When the host leaves - the room closes and the relay disconnects its players. The netcodes in this sample don't migrate the host.
One relay or many - one deployment serves many rooms when players use room codes. With the matchmaker, each relay hosts up to
RELAY_MAX_ROOMSrooms near its players, and a new relay starts only when nearby relays are full.Changes to LightNet - the sample's LightNet works behind NAT and port mapping: the relay answers each player's UDP traffic at the address it came from. See the repository's README for the full list.
Last updated
Was this helpful?

