# ICE

> The ICE (Interactive Connectivity Establishment) is a framework that finds the best network path between two peers.

## What is ICE in WebRTC?

The framework that collects possible addresses of a device, called candidates, and tests them to find a working path between peers. It tries direct connections first, then STUN-discovered addresses, and falls back to a TURN relay.

- Topic: Real-Time Communication
- Type: protocol

## Example

Wrong:

```js
pc.oniceconnectionstatechange = () => {
  console.log(pc.iceConnectionState);
};
```

Right:

```js
pc.oniceconnectionstatechange = () => {
  if (pc.iceConnectionState === 'failed') {
    pc.restartIce();
  }
};
```

`restartIce()` gathers new candidates and triggers renegotiation, so the call can recover after a network change.

## Common mistakes

- Ignoring the 'failed' connection state instead of restarting ICE or showing an error.

## Key points

- The `icecandidate` event handler sends every candidate to the other peer.
- The ICE servers list includes both STUN and TURN servers.

## Interview questions

### What are host, srflx and relay candidates?

Host is the device's local address, srflx is the public address found through STUN, relay is an address on a TURN server.

### What is an ICE restart?

Gathering new candidates and renegotiating on an existing connection, used when the network changes or the connection fails.

### Why does ICE fail to connect?

Usually because no candidate pair can reach the other side, often due to strict NAT or firewalls with no TURN server configured.

## Related cards

- [STUN](https://wonderium.dev/web/stun.md)
- [TURN](https://wonderium.dev/web/turn.md)
- [Signaling](https://wonderium.dev/web/signaling.md)
- [WebRTC](https://wonderium.dev/web/webrtc.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/ice
- Last updated: 2026-11-25
