AgentOption pattern. This guide covers how to configure your ICE agent with STUN/TURN servers, network types, candidate types, timeouts, and other essential settings.
Creating an Agent
There are two ways to create an ICE agent:The
AgentConfig approach is deprecated. Use NewAgentWithOptions with functional options instead.STUN/TURN Server Configuration
Configure STUN and TURN servers to enable server reflexive and relay candidates:TURN Protocols
TURN supports multiple transport protocols:- UDP:
turn://turn.example.com:3478 - TCP:
turn://turn.example.com:3478?transport=tcp - TLS:
turns://turn.example.com:5349?transport=tcp - DTLS:
turns://turn.example.com:5349?transport=udp
Network Types
Control which network types are used for candidate gathering:Available Network Types
NetworkTypeUDP4- UDP over IPv4NetworkTypeUDP6- UDP over IPv6NetworkTypeTCP4- TCP over IPv4 (requires TCPMux)NetworkTypeTCP6- TCP over IPv6 (requires TCPMux)
Candidate Types
Specify which candidate types to gather:Port Range Configuration
Restrict the UDP port range for host candidates:Port range configuration is ignored when using UDPMux, as the mux manages a single port.
Timeout Configuration
Configure various timeouts for connectivity and gathering:Connectivity Timeouts
Gathering Timeout
Candidate Selection Timeouts
Control minimum wait times before nominating each candidate type:Interface and IP Filtering
Filter network interfaces and IP addresses during candidate gathering:ICE Lite Mode
Configure the agent to run in ICE Lite mode:Local Credentials
Set custom ICE username fragment and password:The username fragment must have at least 24 bits of randomness (3 characters), and the password must have at least 128 bits (16 characters). If empty strings are provided, random values will be generated.
Maximum Binding Requests
Control the maximum number of binding requests before marking a pair as failed:Logging
Configure logging for the ICE agent:Proxy Support
Configure a proxy dialer for TURN over TCP/TLS:Complete Example
Here’s a complete example combining multiple configuration options:Default Values
The following table shows the default values for key configuration options:Next Steps
Candidate Gathering
Learn how to gather ICE candidates
Connectivity Checks
Understand how ICE performs connectivity checks
Multiplexing
Share ports with UDPMux and TCPMux
NAT Traversal
Configure NAT traversal and address rewriting