Enable DTLS encryption

Note: Enabling DTLS encryption only enables encryption of message. Other Relay behavior, such as timeouts, remains the same.

Relay supports DTLS encryption of all UDP communication to and from the Relay servers. Set the Relay server connectionType to dtls when creating an allocation as a host player to enable DTLS encryption.

Warning: Secure connections using DTLS are only available with Unity Editor versions 2020.3 (starting at 2020.3.34), 2022.1, and above.

Note: Players joining the host player can use a different connection type than the host player. However, most gaming platforms required encrypted connections.

The following code snippet has a function, AllocateRelayServerAndGetJoinCode, that shows how to use the Relay SDK to create an allocation, request a join code, and configure the connection type as DTLS.

public static async Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string joinCode)> AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null)
{
    Allocation allocation;
    string createJoinCode;
    try
    {
        allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, region);
    }
    catch (Exception e)
    {
        Debug.LogError($"Relay create allocation request failed {e.Message}");
        throw;
    }

    Debug.Log($"server connection data: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
    Debug.Log($"server allocation ID: {allocation.AllocationId}");

    try
    {
        createJoinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    }
    catch
    {
        Debug.LogError("Relay create join code request failed");
        throw;
    }

    var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == "dtls");
    return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, allocation.ConnectionData, allocation.Key, createJoinCode);
}