Interactive Connectivity Establishment or ICE is a protocol that is used to find ways for devices to connect to each other on the internet, so that these devices can communicate with each other directly

The ICE protocol plays an important role in VoIP and video calling using WebRTC protocol.

Here is what we are going to learn inthis article

  • What is ICE
  • Components of ICE
  • STUN Server
  • TURN server
  • Metered TURN servers
  • How ICE Works
  • Step 1: Gethering Candidates
  • Step 2: Prioritizing Candidates
  • Step 3: Checking Connectivity
  • Step 4: Selecting the Best Candidates
  • Setting Up the ICE Server

What is ICE server?

Interactive connectivity establishment (ICE) is a protocol used in network communications to establish direct peer 2 peer communication by the means of overcoming the NAT and firewalls

The ICE protocol helps the webrtc to determine the best possible path for data communication that includes audio, video and data thus ensuring the connectivity is there

The ICE array contains different STUN and TURN server addresses, the webrtc tries to connect through these servers and if one fails it tries the other one. Thus the ICE array tries to connect through the best possible way.

If one server fails, ICE tries to establish the server using other servers in the array

Here is a sample ICE server array

  iceServers: [
    {
      urls: "stun:relay.metered.ca:80",
    },
    {
      urls: "turn:relay.metered.ca:80",
      username: "your-turn-server-username",
      credential: "your-turn-server-password",
    },
    {
      urls: "turn:relay.metered.ca:443",
      username: "your-turn-server-username",
      credential: "your-turn-server-password",
    },
    {
      urls: "turn:relay.metered.ca:443?transport=tcp",
      username: "your-turn-server-username",
      credential: "your-turn-server-password",
    },
  ],
});
               
sample ICE server array

Components of ICE

the ICE is built around three core components that work together to enable effective network traversal enabling real time communication

  • ICE Agents
  • STUN Servers
  • TURN Servers
[Device A] ----> [ICE Agent A] ----> [STUN/TURN Server] ----> Internet
       \              |                       |
        \------> [Gathered Candidates] <-----/
                  |               |
        [Connectivity Checks with Device B]
                  |
        [Best Candidate Path Selected]
                  |
             [Device B]

ICE Agents:

A ICE agent is a software part of ICE protocol that is embedded within a device or application that implements the ICE protocol

The role of this software component is to gether potential network addresses, test connectivities for each and then choose the most suitable one for connection

  • Types
  • Full ICE Agent: A full ICE agent gethers all the candidates and testing pairs
  • Lite ICE Agent: A lite ICE agent gathers limited candidates and testing pairs

STUN Servers

Session Traversal utilities for NAT that is STUN is a protocol used by ICE to facilitate the discovery of a device public IP address and port.

Role:

  • NAT Discovery: Determines what type of NAT the devices are behind
  • Addresses Discovery: Lets the devices that are behind NAT their public IP Address and Port number

TURN servers

Traversal using Relays around NAT (TURN) acts as a intermediate server that relays the data between devices that are behind NAT.

When STUN fails the ICE automatically switches over to TURN server in order to relay traffic between devices

If you are looking for a TURN server for your WebRTC app then you can consider the Metered TURN server service

Metered TURN servers

Metered TURN servers

  1. API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrive Usage data by date via the API.
  2. Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
  3. Servers in 12 Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul
  4. Low Latency: less than 50 ms latency, anywhere across the world.
  5. Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
  6. Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
  7. Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
  8. Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
  9. Enterprise Reliability: 99.999% Uptime with SLA.
  10. Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
  11. 5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
  12. Runs on port 80 and 443
  13. Support TURNS + SSL to allow connections through deep packet inspection firewalls.
  14. Support STUN
  15. Supports both TCP and UDP

How ICE works

ICE is a protocol that manages the process of establishing connections through NAT ( Network address translations) and firewalls. Here is a detailed step by step breakdown of how ICE works

Step 1: Gathering Candidates

Candidates are potential network addresses that can be used to establish the connection. The first step in the ICE process is to gather as many addresses as possible these can then be used to establish connections, if one address fails then ICE tries to establish with another address

These candidates include STUN server and TURN servers, There can be multiple servers available in the ICE array

  • Host Candidates:
  • Server Reflexive Candidates
  • Relayed Candidates

The gethering process involves an ICE agent that queries both STUN and TURN servers

Step 2 Prioritizing Candidates

Once all the candidates are collected, they are prioritized. ICE has a priority formula that is used to assign a numeric priority to each candidate.

This priority number is then used to determine the order in which the candidates are tested. The formula has various considerations such as

  • Is the candidate a host
  • server reflexive or
  • relayed type

This formula assigns a higher value to the candidate that has a higher possibility to successfully establish and maintain the connection.

Generally, host candidates gets that top priority then server reflexive candidates and lastly relayed candidates.

Step 3 Checking Connectivity

Once the prioritizing is done then the ICE agent begins connectivity checks

  • STUN binding requests
  • ICE Continuations

Step 4 Selecting the Best Candidates

Once the connectivity checks are completed ICE needs to determine which candidate pair provides the best performance

  • Nomination of Pairs
  • Final Selection
[Start]
  |
[1. Gather Candidates] ---> [Host] ---> [Server Reflexive] ---> [Relayed]
  |
[2. Prioritize Candidates] ---> [Assign Priority Based on Type]
  |
[3. Check Connectivity] ---> [Send STUN Requests to Each Candidate Pair]
  |                               |
  |------ [Successful?] <---------|
  |          |
[Yes]     [No]
  |          |
[4. Select Best Candidate]    [Continue Checking Lower Priority Candidates]
  |
[End: Establish Connection]
ICE Candidates 

Setting Up an ICE Server: Metered Global TURN servers

When you are using ICE server to establish connections for WebRTC applications. Electing the right tools and ensuring smooth integration are important steps that will improve the effectiveness and reliability of communication

Metered Global TURN servers offer a comprehensive solution due to their extensive features and global reach

Here is a detailed guide on how to set up and integrate these servers with WebRTC.

Metered TURN servers

  1. API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrive Usage data by date via the API.
  2. Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
  3. Servers in 12 Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul
  4. Low Latency: less than 50 ms latency, anywhere across the world.
  5. Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
  6. Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
  7. Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
  8. Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
  9. Enterprise Reliability: 99.999% Uptime with SLA.
  10. Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
  11. 5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
  12. Runs on port 80 and 443
  13. Support TURNS + SSL to allow connections through deep packet inspection firewalls.
  14. Support STUN
  15. Supports both TCP and UDP

Installation and integration

  1. SignUp and API integration

You can create a free account on Metered turn servers by going to https://metered.ca/stun-turn

Next, you can integrate the TURN server API to manage the TURN server creadentials programatically and check on the usage as well

Here is the documentation for that: https://www.metered.ca/docs/turn-rest-api/get-credential

  1. Setting up the TURN server

Using the API you can integrate the TURN server in your webrtc application. IF you do not want to us the API then use can directly add the ICE server array to your WebRTC application as well.

Integration with WebRTC

When creating calls using WebRTC you can add ICE server array to establish peer to peer connections

Here is a example on how you can do it:

const iceServers = [
  {
    urls: "turn:your-turn-server.example.com", // Replace with your Metered TURN server URL
    username: "example-user", // Replace with your Metered username
    credential: "example-password" // Replace with your Metered password
  }
];

const peerConnection = new RTCPeerConnection({
  iceServers: iceServers
});

// Setup to handle events
peerConnection.onnegotiationneeded = async () => {
  try {
    const offer = await peerConnection.createOffer();
    await peerConnection.setLocalDescription(offer);
  } catch (e) {
    console.error(e);
  }
};

// Listen for ICE candidates
peerConnection.onicecandidate = event => {
  if (event.candidate) {
    // Send the candidate to the remote peer
  } else {
    // All ICE candidates have been sent
  }
};
establishing peer to peer connection

Here are some of our other article that might be interest to you:

Conclusion

In this article we learned about how you can establish the ICE server. What are ICE server array and how you can set up the ICE server array with Metered TURN servers.