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

WebRTC Cookbook
By :

Your WebRTC application can work without STUN or TURN servers if all the peers are located in the same plain network. If your application is supposed to work for peers that might be located in different networks, it will definitely need to use at least the STUN server to work.
In this recipe, we will install a STUN server on a Linux box. STUN server can be installed under the other platform as well, but for simplicity, we will consider only the Linux case. So, please prepare a Linux machine.
In this recipe, we will use a very basic and simple STUN server implementation, so you probably will not need to install additional libraries or do some difficult configuration.
STUN needs two IP addresses to work correctly. Thus, when experimenting with your Linux box, take care that the Linux box should have at least two IP addresses that are available for all possible peers (WebRTC clients).
The following set of steps will lead you through the process of configuring and building a STUN service:
tar –xzf stund-0.97.tgz cd stund
make
The last command will build the server. After that, you can start the STUN server by using the following command:
./server -h primary_ip -a secondary_ip
Note that instead of primary_ip
and secondary_ip
, you should use actual IP addresses that are available on the machine. This software can't detect such network parameters automatically, so you need to set it up explicitly.
If you want to start the server in the background, add the -b
option to the preceding command.
Now, when the STUN server is configured and running, we can utilize it in the WebRTC application. When your application wants to create a peer connection object, it uses something like the following code:
var pc; pc = new RTCPeerConnection(configuration);
Here, configuration
is an entity that contains different options for creating peer connection object. To utilize your freshly installed STUN server, you should use something like the following code:
var configuration = { 'iceServers': [ { 'url': 'stun:stun.myserver.com:19302' } ] }
Here we inform the web browser that it can use the STUN server if necessary. Note that you should use the real domain name or IP address of the STUN server. You can also explicitly set the port number as shown in the preceding code, in case it is distinguished from the default value.
STUN server can help peers determine their network parameters and thus establish a direct communication channel. If your clients are located behind NAT or firewall, your application should use at least the STUN service to make the direct connection possible. Nevertheless, in many cases that might not be enough, and using TURN might be necessary.
The following diagram might be helpful to you to imagine how the STUN server is located in the whole infrastructure, and how all the components interoperate with each other:
As an alternative to this, you can use rfc5766-server—it is a free and open source implementation of both STUN and TURN servers. It also supports many additional features that might be quite useful. You can find it at https://code.google.com/p/rfc5766-turn-server/.
Change the font size
Change margin width
Change background colour