Quality of service (QoS)
Use Quality of Service measurements to automatically select the best Relay region for your players.
Read time 1 minuteLast updated 14 hours ago
Relay’s quality of service (QoS) feature allows you to select a region automatically based on quality of service data by starting the NetworkDriver as a host player without selecting a target region.
Creating an allocation request without specifying a region triggers the Allocations service to use quality of service data to select the best available region based on the quality of the connection between each region and the host. Relay considers both latency and packet loss. Check out the sample code below.
The diagram below shows how Relay selects a region based on QoS data between the host player client and the available regions. If Relay can't locate QoS servers or collect measurements, it uses the client's location to pick the nearest region. If the client's location can't be determined, then it resolves to the default region, which is Central US.// Launch this method as a coroutineprivate IEnumerator StartRelayServer(){ // Request an allocation to the Relay service without a target region var relayMaxPlayers = 5; var allocationTask = RelayService.Instance.CreateAllocationAsync(relayMaxPlayers); while(!allocationTask.IsCompleted) { yield return null; } if (allocationTask.IsFaulted) { Debug.LogError("Create allocation request failed"); yield break; } var allocation = allocationTask.Result; // Request the join code to the Relay service var joinCodeTask = RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); while(!joinCodeTask.IsCompleted) { yield return null; } if (joinCodeTask.IsFaulted) { Debug.LogError("Create join code request failed"); yield break; } // Get the Join Code, you can then share it with the clients so they can join var joinCode = joinCodeTask.Result; // Format the server data, based on desired connectionType var relayServerData = HostRelayData(allocation, "dtls"); // Create the network parameters using the Relay server data var relayNetworkParameter = new RelayNetworkParameter{ ServerData = relayServerData }; // Bind and listen to the Relay server yield return ServerBindAndListen(relayNetworkParameter);}

A graph depicting how Relay selects a region for a client. Relay first attempts to select the best region based on quality of service measurements. If these aren't available, Relay next tries to select the nearest location geographically to the client. If this also doesn't work, Relay finally falls back to a default region.