Overview
Host candidates represent direct network interfaces on the local machine. They are the most preferred type of candidate because they enable direct peer-to-peer communication without intermediary servers.CandidateHost
A host candidate is obtained directly from a local network interface.candidate_host.go
candidateBase which provides common candidate functionality including priority calculation, marshaling, and traffic tracking.
Creating Host Candidates
NewCandidateHost
Creates a new host candidate from the provided configuration.Configuration for the host candidate.
CandidateHost or an error if the configuration is invalid.
CandidateHostConfig
Configuration structure for creating host candidates.candidate_host.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”.
IP address of the local interface. Can be an IPv4 address, IPv6 address, or mDNS name ending in “.local” or “.invalid”.
Port number for the candidate.
Component identifier. Use
ComponentRTP (1) for RTP or ComponentRTCP (2) for RTCP.Custom priority value. If 0, priority will be calculated automatically according to RFC 8445.
Custom foundation string. If empty, foundation will be calculated as a CRC32 checksum of the candidate type, address, and network type.
For TCP candidates, specifies the TCP type:
TCPTypeActive, TCPTypePassive, or TCPTypeSimultaneousOpen. Use TCPTypeUnspecified for UDP candidates.Whether this candidate should be filtered for location tracking purposes.
Examples
Creating a UDP Host Candidate
Creating a TCP Host Candidate
Creating an IPv6 Host Candidate
Creating an mDNS Host Candidate
mDNS candidates (addresses ending in “.local” or “.invalid”) are not immediately resolved. The network type is assumed to be UDP4 until the address is resolved.
When to Use Host Candidates
Host candidates should be used when:- Establishing direct peer-to-peer connections
- Both peers are on the same local network
- NAT traversal is not required
- Minimizing latency is a priority
- Privacy is important (no third-party servers involved)
Priority
Host candidates have the highest type preference (126) among all candidate types, making them the most preferred for connection establishment. The full priority calculation is:TCP Type
For TCP host candidates, theTCPType field specifies the connection behavior:
The candidate initiates TCP connections (acts as a client).
The candidate only accepts TCP connections (acts as a server).
The candidate can both initiate and accept connections simultaneously.
Related Address
Host candidates do not have a related address, as they represent the actual local interface. TheRelatedAddress() method returns nil for host candidates.
Common Use Cases
Local Network Communication
Host candidates are ideal for applications where peers are on the same LAN:Multi-homed Systems
For systems with multiple network interfaces, create a host candidate for each:See Also
- Candidate Types - Core candidate interfaces and types
- Server Reflexive Candidates - For NAT traversal
- Relay Candidates - For restrictive network environments