Quality of service (QoS)

Note: QoS currently doesn't work if you're using Relay with WebSockets/WebGL.

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.

Note: Not all Unity Editor versions support QoS. Make sure you’re using one of the following versions if you plan to use QoS SDK:

  • 2022.2.0a10+
  • 2022.1.0f1+
  • 2021.3.2f1+
  • 2020.3.34f1+

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.

// Launch this method as a coroutine
private 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
  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);
}

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 will use the client's location to pick the nearest region. If the client's location can't be determined then it will resolve to the default region, which is Central US.