Book Image

WebRTC Cookbook

By : Andrii Sergiienko
Book Image

WebRTC Cookbook

By: Andrii Sergiienko

Overview of this book

Table of Contents (15 chapters)
WebRTC Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Configuring and using STUN


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.

Getting ready

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).

How to do it…

The following set of steps will lead you through the process of configuring and building a STUN service:

  1. Download the STUN server from its home page at http://sourceforge.net/projects/stun/.

  2. Unpack the archive and go into the STUN server folder:

    tar –xzf stund-0.97.tgz
    cd stund
    
  3. Build it with the following command:

    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.

Tip

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.

How it works…

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:

There's more…

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/.

See also

  • For details on how STUN works, you can refer to RFC #3489 http://www.ietf.org/rfc/rfc3489.txt.

  • In the Configuring and using TURN recipe, we will use a TURN server based on the rfc5766-server software. That application can serve as a STUN server as well.