WebRTC: the ICE Framework, STUN and TURN Servers

Heloise Bahadiroglu
Level Up Coding
Published in
5 min readMar 23, 2020

--

WebRTC (Web Real Time Communication) is an open source project enabling the creation of peer-to-peer (P2P) audio and video communications through a JavaScript API.

In order for a P2P connection to be established, peers have to communicate about the media types they want to exchange, to tell each other when they want to start or stop the communication, and they have to find each other in the network. The complete process is called signaling, but we are interested in the last part only: how to connect peers as directly as possible.

This is not as easy as it sounds. The users’ devices usually don’t have a public IP address or might not be allowed to establish any direct connection. This is why we need the Interactive Connectivity Establishment (ICE) framework.

NATs

If the two users were making a phone call, one would only have to dial the other’s phone number and the other would only have to accept the call. Each phone number corresponding to one device only, phone numbers are enough to enable a direct connection.

On the internet on the other hand, there was historically not enough “numbers” for every connected device. With IPv4, there were only around 4 billions addresses available. The lack of addresses was solved by grouping many devices under one public address, with a router translating addresses in the packets going through it. This process is called Network Address Translation (NAT).

There are different types of NATs, but some of them allocates a public IP address and a port for UDP flows (what we need). When you want to create a P2P connection with a peer, a first challenge is therefore to discover what kind of NATs you are behind, and if they exist, to get an IP address and a port you can give to your contact.

STUN

The STUN (Session Traversal Utilities for NAT) protocol helps you do so. You have to provide the STUN server when trying to establish a P2P connection. In WebRTC, you provide it when creating the JavaScript object representing the connection:

--

--

Freelance Software Engineer. I write about web and cloud technologies. AWS and Azure certified.