Skip to main content

Overview

The Agent represents the ICE agent responsible for gathering candidates, performing connectivity checks, and establishing peer-to-peer connections according to RFC 5245.

Creating an Agent

NewAgent (Deprecated)

Creates a new Agent using the legacy configuration struct.
Deprecated: Use NewAgentWithOptions instead for better flexibility and forward compatibility.
Parameters:
*AgentConfig
Configuration for the agent. See AgentConfig for details.
Returns: (*Agent, error)

NewAgentWithOptions

Creates a new Agent using functional options. This is the recommended way to create agents. Parameters:
...AgentOption
Variable number of configuration options. See Agent Options for available options.
Returns: (*Agent, error) Example:

Key Methods

GatherCandidates

Starts the candidate gathering process. This discovers available local addresses and queries STUN/TURN servers.

GetLocalCandidates

Returns all gathered local candidates.

AddRemoteCandidate

Adds a remote candidate received from the peer. This triggers connectivity checks for candidate pairs. Parameters:
Candidate
Remote candidate to add. Can be nil (ignored).

GetSelectedCandidatePair

Returns the currently selected candidate pair, or nil if no pair is selected.

SetRemoteCredentials

Sets the remote agent’s ICE credentials. Parameters:
string
required
Remote username fragment (must not be empty)
string
required
Remote password (must not be empty)

GetLocalUserCredentials

Returns the local ICE credentials (username fragment and password).

GetRemoteUserCredentials

Returns the remote ICE credentials that have been set via SetRemoteCredentials.

GetRemoteCandidates

Returns all remote candidates that have been added to the agent.

Dial

Connects to the remote agent in controlling mode. Returns a Conn that can be used to send and receive data. Parameters:
context.Context
Context for controlling the dial operation lifetime.
string
Remote username fragment (from remote agent’s GetLocalUserCredentials).
string
Remote password (from remote agent’s GetLocalUserCredentials).
Returns: (*Conn, error) - Connection for data transfer Example:

Accept

Waits for connection from the remote agent in controlled mode. Returns a Conn that can be used to send and receive data. Parameters:
context.Context
Context for controlling the accept operation lifetime.
string
Remote username fragment (from remote agent’s GetLocalUserCredentials).
string
Remote password (from remote agent’s GetLocalUserCredentials).
Returns: (*Conn, error) - Connection for data transfer

Restart

Restarts the ICE agent with new credentials. If empty strings are provided, the agent generates new credentials. Parameters:
string
New username fragment. Empty string generates a random value.
string
New password. Empty string generates a random value.
Credentials must meet minimum entropy requirements: ufrag >= 24 bits, pwd >= 128 bits.

UpdateOptions

Applies options to the agent at runtime. Only a subset of options can be updated after creation (e.g., WithUrls).

Close

Cleans up the agent and releases resources. Does not wait for goroutines to complete.

GracefulClose

Cleans up the agent and waits for all goroutines to complete. Should only be called outside of agent callbacks or in a separate goroutine.

State Management

GetGatheringState

Returns the current gathering state. See GatheringState.

OnConnectionStateChange

Registers a handler that is called when the connection state changes.

OnSelectedCandidatePairChange

Registers a handler called when the selected candidate pair changes.

OnCandidate

Registers a handler called when a new local candidate is gathered. Called with nil when gathering is complete.

Renomination

RenominateCandidate

Allows the controlling agent to nominate a new candidate pair. Requires renomination to be enabled. Returns: Error if agent is not controlling, renomination is disabled, or candidate pair not found.

Type Definition