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
The protocol used between the endpoint and the relay server (“udp”, “tcp”, “dtls”, or “tls”).
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.Configuration for the relay candidate.
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
Unique identifier for the candidate. If empty, a UUID will be automatically generated.
Network protocol: “udp”, “tcp”, “udp4”, “udp6”, “tcp4”, or “tcp6”.
The relayed transport address allocated by the TURN server (public IP address).
Port number of the relayed address.
Component identifier. Use
ComponentRTP (1) for RTP or ComponentRTCP (2) for RTCP.Custom priority value. If 0, priority will be calculated automatically. Relay candidates have the lowest type preference (0).
Custom foundation string. If empty, foundation will be calculated based on the candidate type, address, and network type.
The base address (host candidate address) from which this relay candidate was derived.
The port of the base address.
Protocol used for communication between client and TURN server: “udp”, “tcp”, “tls”, or “dtls”.
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.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:- Connect to TURN Server - Establish a connection using the TURN protocol
- Allocate Address - Request an address allocation from the TURN server
- Create Relay Candidate - Create a candidate with the allocated address
- 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:Preference: 3 - Highest preference for relay protocols. Provides best performance.
Preference: 2 - Encrypted UDP transport.
Preference: 1 - Reliable transport but higher overhead.
Preference: 0 - Encrypted TCP transport with highest overhead.
Priority Calculation
Relay candidates have the lowest type preference (0) among all candidate types: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.
Related Address
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
- 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
- Candidate Types - Core candidate interfaces and types
- Host Candidates - Direct local interface candidates
- Server Reflexive Candidates - STUN-based candidates