Skip to main content

Overview

Relay candidates are obtained from TURN (Traversal Using Relays around NAT) servers and provide a fallback mechanism when direct peer-to-peer connections are not possible. All media flows through the TURN server, ensuring connectivity even in the most restrictive network environments.

CandidateRelay

A relay candidate represents an allocated address on a TURN server.
candidate_relay.go
string
The protocol used between the endpoint and the relay server (“udp”, “tcp”, “dtls”, or “tls”).
func() error
Optional callback function invoked when the candidate is closed, allowing cleanup of TURN allocations.

Creating Relay Candidates

NewCandidateRelay

Creates a new relay candidate from the provided configuration.
*CandidateRelayConfig
required
Configuration for the relay candidate.
Returns the created CandidateRelay or an error if the configuration is invalid (e.g., invalid IP address).

CandidateRelayConfig

Configuration structure for creating relay candidates.
candidate_relay.go

Configuration Fields

string
Unique identifier for the candidate. If empty, a UUID will be automatically generated.
string
required
Network protocol: “udp”, “tcp”, “udp4”, “udp6”, “tcp4”, or “tcp6”.
string
required
The relayed transport address allocated by the TURN server (public IP address).
int
required
Port number of the relayed address.
uint16
required
Component identifier. Use ComponentRTP (1) for RTP or ComponentRTCP (2) for RTCP.
uint32
Custom priority value. If 0, priority will be calculated automatically. Relay candidates have the lowest type preference (0).
string
Custom foundation string. If empty, foundation will be calculated based on the candidate type, address, and network type.
string
required
The base address (host candidate address) from which this relay candidate was derived.
int
required
The port of the base address.
string
required
Protocol used for communication between client and TURN server: “udp”, “tcp”, “tls”, or “dtls”.
func() error
Optional callback invoked when closing the candidate to clean up TURN allocations.

Methods

RelayProtocol

Returns the protocol used between the endpoint and the relay server.
string
The relay protocol: “udp”, “tcp”, “tls”, or “dtls”.

Examples

Creating a UDP Relay Candidate

Creating a TLS Relay Candidate

Relay Candidate with Cleanup Handler

TURN Server Integration

Relay candidates are typically created after successfully allocating an address on a TURN server. The integration process involves:
  1. Connect to TURN Server - Establish a connection using the TURN protocol
  2. Allocate Address - Request an address allocation from the TURN server
  3. Create Relay Candidate - Create a candidate with the allocated address
  4. Use for ICE - Add the candidate to your ICE agent for connectivity checks

Example Integration Flow

Relay Protocol Preferences

The relay protocol affects the local preference calculation, influencing candidate priority:
protocol
Preference: 3 - Highest preference for relay protocols. Provides best performance.
protocol
Preference: 2 - Encrypted UDP transport.
protocol
Preference: 1 - Reliable transport but higher overhead.
protocol
Preference: 0 - Encrypted TCP transport with highest overhead.

Priority Calculation

Relay candidates have the lowest type preference (0) among all candidate types:
The relay local preference is determined by the RelayProtocol:
  • UDP: 3 (highest)
  • DTLS: 2
  • TCP: 1
  • TLS: 0 (lowest)

When to Use Relay Candidates

Relay candidates should be used when:
  • Direct peer-to-peer connections fail
  • Both peers are behind symmetric NATs
  • Firewall rules block direct connections
  • Server reflexive candidates fail
  • Guaranteed connectivity is required
Relay candidates consume server resources and bandwidth. Use them as a fallback mechanism when other candidate types cannot establish connectivity.
Relay candidates include a related address that indicates the base (host) address from which the relay was obtained. This information is useful for diagnostics:

Connection Characteristics

Advantages:
  • Works in all network environments
  • Guaranteed connectivity
  • Supports both UDP and TCP
  • Can traverse any NAT or firewall
Disadvantages:
  • Higher latency (media flows through TURN server)
  • Increased bandwidth costs (server relays all traffic)
  • Lower quality compared to direct connections
  • Requires TURN server infrastructure

See Also