Skip to main content
This guide references examples from the Pion ICE repository, explaining key patterns and use cases.

Available Examples

The Pion ICE library includes several examples demonstrating different features:

Ping-Pong

Basic peer-to-peer connection with message exchange

Continual Gathering

Dynamic candidate gathering with network monitoring

Automatic Renomination

Automatic switching to better candidate pairs

NAT Rules

Address rewriting for complex NAT scenarios

Ping-Pong Example

The ping-pong example demonstrates a basic ICE connection between two peers.

Key Patterns

Setting up the agent:
examples/ping-pong/main.go
Handling candidates:
examples/ping-pong/main.go
Establishing connection:
examples/ping-pong/main.go
Exchanging data:
examples/ping-pong/main.go

Running the Example

Continual Gathering Example

Demonstrates dynamic candidate gathering with network interface monitoring.

Key Patterns

Configuring continual gathering:
examples/continual-gathering/main.go
Tracking candidates over time:
examples/continual-gathering/main.go
Monitoring gathering state:
examples/continual-gathering/main.go

Running the Example

Try connecting/disconnecting network interfaces while running with -mode=continually to see new candidates discovered automatically.

Automatic Renomination Example

Shows automatic switching to better candidate pairs based on network conditions.

Key Patterns

Enabling automatic renomination:
examples/automatic-renomination/main.go
Monitoring candidate pair changes:
examples/automatic-renomination/main.go
Tracking RTT:
examples/automatic-renomination/main.go
Debugging candidate pairs:
examples/automatic-renomination/main.go

Running the Example

This example requires network namespaces to simulate network changes:
The example uses tc (traffic control) to add latency to specific interfaces, triggering automatic renomination to better paths.

NAT Rules Example

Demonstrates address rewriting for various NAT scenarios.

Key Patterns

Multi-network host rewriting:
examples/nat-rules/main.go
Interface-scoped rules:
examples/nat-rules/main.go
Server reflexive pool:
examples/nat-rules/main.go
CIDR-scoped rules:
examples/nat-rules/main.go

Running the Example

The Docker Compose setup creates a realistic multi-homed environment with multiple networks and NAT gateways.

Common Patterns

Error Handling

All examples demonstrate proper error handling:

Resource Cleanup

Always close the agent when done:

Signaling

Examples use HTTP for simplicity, but production should use WebSocket or other real-time protocols:

Logging

Enable debug logging to understand behavior:

Building and Running

All examples can be run directly:

Troubleshooting Examples

Ping-Pong Connection Fails

  • Ensure both instances are running
  • Check they’re using opposite controlling flags
  • Verify HTTP ports are available (9000, 9001)
  • Enable debug logging to see connection progress

Continual Gathering Shows No New Candidates

  • Try actually changing network interfaces (enable/disable WiFi)
  • Check the monitoring interval isn’t too long
  • Verify interface filters aren’t excluding new interfaces

Automatic Renomination Doesn’t Switch

  • Ensure both agents support renomination
  • Check that network conditions actually changed (use tc to add latency)
  • Verify renomination interval has elapsed
  • Review candidate pair statistics for RTT differences

NAT Rules Not Applied

  • Verify local addresses match exactly
  • Check rule precedence (more specific first)
  • Enable debug logging to see which rules match
  • Ensure external IPs are valid

Next Steps

Configuration

Deep dive into agent configuration options

Gathering

Learn about candidate gathering in detail

Connectivity Checks

Understand connectivity check mechanics

GitHub Repository

Browse the full source code and examples