-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

WebRTC Cookbook
By :

In most cases, it is enough to use a STUN server to establish a peer-to-peer direct connection. Nevertheless, you will often need to utilize TURN servers—mostly for clients located in big companies (because of firewall policy and tricky NAT) and some specific countries (because of firewalls and access limits).
In this section, we will download, install, and do the basic configuration of a TURN service. Then, we will utilize it in our WebRTC application. A TURN server can be installed under different platforms, although we will cover a Linux box use case only. Thus, for this recipe, you will need a Linux box installed.
For this recipe, we will use rfc5766-turn-server—a free and open source implementation of the TURN and STUN servers. Download its source code from its home page at https://code.google.com/p/rfc5766-turn-server/.
First, we will shortly cover the installation and basic configuration of the TURN server. After that, we will learn how to use it in the application.
If you have TURN server already installed, you can skip this section and go directly to the next one.
I assume that you have downloaded rfc5766-server already and unpacked it. So, let's install and configure your own TURN server:
rfc5766-server
folder with the following command:cd ~/turnserver-4.1.2.1
./configure make sudo make install
Note that rfc5766-server needs some libraries that might be not installed on your system—in particular, libssl-dev
, libevent-dev
, and openssl
. You should install the absent libraries to compile the software successfully.
turnserver
You will see diagnostic messages in the console:
0: ===========Discovering relay addresses: ============= 0: Relay address to use: x.x.x.x 0: Relay address to use: y.y.y.y 0: Relay address to use: ::1 0: ===================================================== 0: Total: 3 relay addresses discovered 0 0: =====================================================
To stop the server, just press Ctrl + C; you will get back to console.
Now it is time to perform some configuration steps and tune your fresh TURN server for your requirements.
By default, the TURN server doesn't have any configuration file. We need to create this configuration file from the default configuration file supplied with the server:
sudo cp /usr/local/etc/turnserver.conf.default /usr/local/etc/turnserver.conf
Open the turnserver.conf
file and edit it according to your requirements. We will not cover all the TURN options here, but just basic configuration items that might be important:
listening-ip=
Note that the TURN server needs at least two public IP addresses to operate correctly.
listening-ip
and the second one relay-ip
.relay-ip=
verbose
in the configuration file. If you would like to refer to more details, you should write the word with capital V—Verbose
—so the server will print as much debugging details as possible.no-auth
In this recipe, we haven't covered TURN authentication—this topic is covered in Chapter 2, Supporting Security.
At this stage, you have your own TURN server with basic configuration, which can be used in WebRTC applications.
When you create a peer connection object, you usually use some construction like the following one:
var pc; pc = new RTCPeerConnection(configuration);
Here, configuration
is an entity that contains different options to create a peer connection object. To utilize your TURN server, you should use something like the following:
var configuration = { 'iceServers': [ { 'url': 'stun:stun.l.google.com:19302' }, { 'url': 'turn:turn1.myserver.com:3478?transport=udp', }, { 'url': 'turn:turn2.myserver.com:3478?transport=tcp', 'credential': 'superuser', 'username': 'secretpassword' } ] }
Here, we will ask the WebRTC API (actually, we will ask the web browser) to use one of three ways when establishing a peer connection:
Note that you can ask the web browser to use a UDP or TCP protocol while establishing a peer connection through the TURN server. To do that, set up the transport parameter as shown in the preceding bullet points.
In some cases, when clients use NAT and firewalls, it is impossible to establish a peer connection using STUN. This situation often appears when a client is located in a corporative network with a strict policy. In such a case, the only way to establish the connection is to use the TURN server.
The TURN server works as a proxy—all the data between peers (including audio, video, and service data) goes through the TURN server.
The following diagram shows how all the components operate with each other:
In this recipe, we covered only one TURN solution, open source and popular, but there are other solutions in the world that could also be suitable for you:
Of course, there are even more different solutions and services available.
Change the font size
Change margin width
Change background colour