What is a TURN Server?
Tags: webdev, javascript, webrtc, programming
Published: 2023-11-25 01:58:50

In this article we are going to learn about TURN servers and their role in network traversal

* What is a TURN servers?
* How does TURN servers work?
* STUN server VS TURN server
* How to setup a TURN Server?
* TURN Server Providers: An Overview
* Open Relay Project (Free TURN server)
* Metered TURN server (Paid TURN server options)
* Setup your own TURN server
* Setting Up TURN server on cloud providers AWS, Google Cloud, Azure and others
* Use Cases in Various industries

## What is a TURN server

TURN stands for Traversal using relays around NAT, these servers are vital for modern internet communications. 

In various situations where direct peer-to-peer communications is not possible due to network restrictions

Here are some of the functions that TURN servers perform

* **Overcome NAT and firewall issues:** NAT is a method that is used by routers to relay traffic from multiple devices that are connected in a local network through a Single public IP address. NAT is essential because of finite availability to public IP addresses. 

NAT complicates P2P communications since the devices behind a NAT are not directly reachable to the outside network here TURN servers help relay the data

* **Facilitate Communications:** Due to NAT or firewall restrictions situations arise when devices that are behind different NATs and firewall cannot communicate with each other

* where direct communications between devices is not possible. These kinds of restrictions are common in many networks. The TURN servers then facilitate this communication

## How does TURN servers work?

There is a conceptual understanding of turn server and then there is a practical implementation. We will look into both of these concepts, first let us understand conceptually how turn servers work then we will move on to the practical implementation

1. **Relaying Data:** The main purpose of TURN server is to relay data between devices that cannot directly connect to each other due to NAT and firewalls

2. The TURN server recives data from one device and forwards that data to another device. All the data that is transfered through the TURN server is encrypted, so no one, not even the TURN server know what data is being transferred through it

3. **Maintaining the connection: **The TURN server keeps track of hte connections and manages the continues relaying of data through it in order to maintain all the connections which are essential for webRTC communications

## STUN server VS TURN server

**TURN**

* TURN servers are more versatile does handle situations where the STUN server fails
They continuously relay traffic between communicating devices

* They are used where NAT and firewalls restrict communication through STUN servers
Requires a lot of bandwidth, CPU and resources

**STUN**

* STUN servers are used to descover local IP addresses and port number that the NAT has assigned to a specific user

* STUN servers help in establishing a direct connection between devices that are behind two different NATs

* STUN is a simpler protocol and fails in symmetric NAT and when complex firewall rules are present 

## Cloud Turn server Providers: An Overview

There are various TURN server providers in the market today. We will look at both free and paid versions available and we will also consider the option of running the TURN server ourselves in the Cloud with the help of Cloud servers like AWS and Google Cloud



## [Metered Global TURN servers​](https://www.metered.ca/stun-turn)

- **Global Geo-Location targeting:** Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance.
- **Servers in 12 Regions of the world:** Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney
- **Low Latency:** less than 50 ms latency, anywhere across the world.
- **Cost-Effective:** pay-as-you-go pricing with bandwidth and volume discounts available.
- **Easy Administration:** Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
- **Standards Compliant:** Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
- **Multi‑Tenancy:** Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
- **Reliability:** 99.999% Uptime with SLA.
- **Enterprise Scale:** : With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
- **50 GB/mo Free:** Get 50 GB every month free TURN server usage with the Free Plan

You can create an account on Metered website and get 50GB of Free TURN usage every month


You can Sign Up for [Metered TURN server here](https://www.metered.ca/stun-turn)


Benefits of using a TURN server service provider

1. **Easy to setup and manage**
It is quite use the pre setup server, just add the credentials in your application and the TURN server starts automatically.
2. **Maintenance free**
The TURN server provider does all the maintenance and the TURN server is maintenance free from the users perspective
3. **Scalability**
TURN server from any TURN server provider is highly scalable becuse the TURN server provider is in the business of making available TURN servers that are scalable
4. **Reliability and Uptime**
Turn server providers such as Open Relay Project and Metered TURN servers offer 99.999% Uptime. That is the 5 nine availability
5. **Support and expertise**
TURN server providers such as Metered or Open Relay Project offer dedicated support

## Setting Up TURN server on cloud providers AWS, Google Cloud, Azure and others

You can also setup your own turn server in one of the cloud providers. To run your server you can use the open source Coturn project.

But before we start the tutorial on how to set up your own turn server here are some consideration that you might need to take into account

you can also refer to various detailed guides on how to set up the TURN server on various cloud providers

- [Setup TURN server using CoTURN](https://www.metered.ca/blog/coturn/)
- [AWS TURN server: 7 Simple steps](https://www.metered.ca/blog/aws-turn-server-in-7-simple-steps/)
- [How to setup Google Cloud TURN server](https://www.metered.ca/blog/google-turn-server-in-7-simple-steps/)
- [Azure TURN server the complete guide](https://www.metered.ca/blog/azure-turn-server/)

### Considerations when running your own turn server in a cloud provider

1. Installation
2. Maintenance 
3. Instance Limbo Problems
4. Cost estimation and control
5. Allocation of resources

### Step 1 Choose a Cloud service Provider

you can choose a cloud service provider from a variety of different options like AWS, Google Cloud, Azure and others.

Some of these also offer a free tier for a limited period of time as well.

**Create a Virtual Machine (VM):** Create a VM on your selected cloud provider and ensure that the machine has a public IP address and ports 3478 are open for TCP

remember to install linux debian or ubuntu into your machine

## Step 2 Install Coturn

SSH into your VM and type the below commands to install coturn into your machine

* update the dependencies

```bash
sudo apt-get update
```
```bash
sudo apt-get upgrade
```

* Install the coturn

```bash
sudo apt-get install coturn
```

## Step 3 Configure Coturn

now cd into the coturn folder and 

* edit the configuration file `/etc/turnserver.conf` using nano like so 

```bash
nano /etc/turnserver.conf
```

* Basic configuration

```bash
listening-port=3478
# Use your server's public IP
external-ip=<Your_VM_Public_IP>
# Set a user for authentication
user=<username>:<password>
# Enable log file
log-file=/var/log/turnserver.log
```

This is a basic guide for detailed guide, for complete guide refer to 

* Setup TURN server using CoTURN

## Step 4 Start the TURN server

you can start the turn server with

```bash
turnserver -c /etc/turnserver.conf
```

## Step 5: Integrate with WebRTC

You can easily integrate the TURN server with your webRTC application by specifying your TURN credentials in your ICE server array

```bash
const iceConfig = {
  'iceServers': [
    {
      'urls': 'turn:<Your_VM_Public_IP>',
      'username': '<username>',
      'credential': '<password>'
    }
  ]
};
```

## Step 6 Test the TURN server

Once you have started the TURN server then you can check if the TURN server is working properly or not by going to [https://turndemo.metered.ca/](https://turndemo.metered.ca/) and adding your credentials

## Use Cases in various industries


Telecommunications and VoIP services
The VoIP services TURN servers are necessary for enabling calls and video chats
2. Video Conferencing
For video conferencing especially video conferencing among large number of participants, a TURN server is required
3. IoT devices
If you need communications between IoT devices that are behind NAT, then you need a TURN server to communicate between the devices
4. Streaming services
Video streaming services also require TURN servers to channel the stream to millions of devices that are behind different NAT and firewall rules
5. Remote work and collaboration tools
Many Remote work and collboration tools such as chat software and other communication devices and software require TURN servers to communicate to each other  behind NAT