TURN Server is a VoIP media traffic NAT traversal server and gateway. Traversal Using Relays around NAT is called TURN in short. According to Wikipedia, it is a protocol that assists in the traversal of network address translators or firewalls for multimedia applications. You can assume it as a relay point.

If you are very new to networking, you may not get the term NAT. NAT refers to Network address translation. It is a method of remapping one IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device. I have picked this definition from Wikipedia. You may go through this article to know more about NAT.

Okay, let’s get back to our main topic. Recently I have taken a very little part in building a video chatting app. This app uses STUN (Session Traversal Utilities for NAT) server for peer-to-peer communication. STUN is a standardized set of methods, including a network protocol, for NAT gateways in applications of real-time voice, video, messaging, and other interactive communications.

Then, why are we talking about TURN server? TURN server is a STUN server with added relaying functionality built in. It is used to relay traffic if the peer-to-peer communication fails. We may assume an example here. We are trying to go to the USA with a direct flight. This refers to STUN. But unfortunately we have missed the flight and now we’re going through Dubai. This refers to TURN.

We have learned a lot about TURN and STUN. This is enough for the time being. Now we will dive into installing a TURN server. There are several STUN/TURN servers: coturn, restund, Xirsys, Twilio (Uber uses it). Among them, coturn is free, open source and very popular. We will install it by compiling from source code to get our very own TURN server. You may find PPA or deb package for Ubuntu. You can install using them.

Let’s become the superuser and make sure our system has the latest updates:

$ apt update
$ apt upgrade -y

We will need some dependencies for compiling the source. Let’s install them.

$ apt-get update && apt-get install libssl-dev libevent-dev libhiredis-dev make wget unzip -y

Now we have to find out the latest version of coturn from its release page. When I have been writing this blog post, coturn 4.5.0.7 has been the latest version. Let’s download and unzip it.

$ wget https://github.com/coturn/coturn/archive/4.5.0.7.zip
$ unzip 4.5.0.7.zip

Now it is the time to install it. Simply play these commands:

$ cd coturn-4.5.0.7/
$ ./configure
$ make
$ make install

Cheers! We’ve installed coturn. The coturn --help command will show you a quick guide how to use it. But we’ll launch the server with the following command:

$ turnserver -a -o -v -n  --no-dtls --no-tls -u maateen:123456 -r "test_realm"

You may wonder what this command means. This wiki can help you at this matter. But we want to mention that -u maateen:123456 refers to the username (maateen) and password (123456) when you will use this server in your WebRTC (Web Real-Time Communication) app. Here’s a demo:

var config = {
  servers: [{
    urls: 127.0.0.1:3478,
    username: 'maateen',
    password: '123456'
  }]
}

127.0.0.1 refers to the host’s local IP. You may use your server’s hostname or public IP in case of using it from a remote server. Also, you may be interested in Supervisor to keep the server process up and running.