Installing a TURN Server on Ubuntu for WebRTC

37,068

Solution 1

it is easy to install in linux machines, not tried in other OSes.

simple way:

sudo apt-get install coturn

If you say no, I want the latest cutting edge, you can download source code from their downloads page in install it yourself, example:

sudo -i     # ignore if you already in admin mode
apt-get update && apt-get install libssl-dev libevent-dev libhiredis-dev make -y    # install the dependencies
wget -O turn.tar.gz http://turnserver.open-sys.org/downloads/v4.5.0.3/turnserver-4.5.0.3.tar.gz     # Download the source tar
tar -zxvf turn.tar.gz     # unzip
cd turnserver-*
./configure
make && make install 

sample command for running TURN server:

sudo turnserver -a -o -v -n  --no-dtls --no-tls -u test:test -r "someRealm"

command description:

  • -a - Use long-term credentials mechanism
  • -o - Run server process as daemon
  • -v - 'Moderate' verbose mode.
  • -n - no configuration file
  • --no-dtls - Do not start DTLS listeners
  • --no-tls - Do not start TLS listeners
  • -u - user credentials to be used
  • -r - default realm to be used, need for TURN REST API

check this wiki for more details and configurations.

now you can use the TURN server in your WebRTC application as:

var peerConnectionConfig = {
  iceServers: [{
    urls: YOUR_IP:3478,
    username: 'test',
    password: 'test'
  }]
}

Solution 2

On your ubuntu server machine, set up, configure & run a packaged version of coturn. For a basic setup, do

# set up
sudo apt-get install --assume-yes coturn

# configure & run
USERNAME="some-username"
PASSWORD="some-password"
PORT=3478

# -n: use only commandline parameters, no config file
sudo turnserver \
    -n \
    --verbose \
    --lt-cred-mech \
    --user $USERNAME:$PASSWORD \
    --realm "someRealm" \
    --no-dtls \
    --no-tls \
    --listening-port $PORT

Add --daemon to keep it running in the background. See https://github.com/coturn/coturn/wiki/turnserver for the list of options of turnserver and have a look at their example config file if you want to use one with -c CONFIGFILE instead of using -n and passing all options on the commandline like I did above.

To check that it worked, in Google Chrome, while on any page of a secure origin (for example stackoverflow.com), run this in the developer console:

function checkTURNServer(turnConfig, timeout){ 

  return new Promise(function(resolve, reject){

    setTimeout(function(){
        if(promiseResolved) return;
        resolve(false);
        promiseResolved = true;
    }, timeout || 5000);

    var promiseResolved = false
      , myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection   //compatibility for firefox and chrome
      , pc = new myPeerConnection({iceServers:[turnConfig]})
      , noop = function(){};
    pc.createDataChannel("");    //create a bogus data channel
    pc.createOffer(function(sdp){
      if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
        promiseResolved = true;
        resolve(true);
      }
      pc.setLocalDescription(sdp, noop, noop);
    }, noop);    // create offer and set local description
    pc.onicecandidate = function(ice){  //listen for candidate events
      if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1))  return;
      promiseResolved = true;
      resolve(true);
    };
  });   
}

const USERNAME="some-username"
const PASSWORD="some-password"
const PORT=3478
const IP="10.11.0.115" // you will have to change this

console.log('TURN server reachable on TCP?', await checkTURNServer( {
    url: `turn:${IP}:${PORT}?transport=tcp`,
    username: USERNAME,
    credential: PASSWORD,
}))

console.log('TURN server reachable on UDP?', await checkTURNServer( {
    url: `turn:${IP}:${PORT}?transport=udp`,
    username: USERNAME,
    credential: PASSWORD,
}))

You should get

TURN server reachable on TCP? true
TURN server reachable on UDP? true

Solution 3

I think the guide is somewhat outdated.

Look at this Google open source TURN server.
Really easy to install and works very well.
https://code.google.com/p/rfc5766-turn-server/

Solution 4

This link will provide all details regarding installation and configuration of TURN server.

https://www.webrtc-experiment.com/docs/TURN-server-installation-guide.html

The guy has very good repository for WebRtc demos.

Share:
37,068

Related videos on Youtube

Dvlpr
Author by

Dvlpr

Updated on September 20, 2020

Comments

  • Dvlpr
    Dvlpr over 3 years

    How can I install a TURN server on my ubuntu 12.04? Can you share tutorial? I read this tutorial: Implementing our own STUN/TURN server for WebRTC Application. But what I don't understand is how I can I install my own TURN server on my ubuntu 12.04?

    I am using currently using something like the following code to create the RTCPeerConnection

    const pc_config = {"iceServers": [{"url": "stun:stun.l.google.com:19302"},
      {"url":"turn:my_username@<turn_server_ip_address>", "credential":"my_password"}]};
    
    const pc_new = new webkitRTCPeerConnection(pc_config);
    

    And I want to fill the above code's arguments to work with a different network.

    When i want to install turn server then I get

    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    E: Unable to locate package resiprocate-turn-server
    

    I used apt-get install resiprocate-turn-server. I also used this https://www.webrtc-experiment.com/docs/TURN-server-installation-guide.html tutorial.

    • Benjamin Trent
      Benjamin Trent over 9 years
      this shows minimal effort in actually trying to install a simple turn server....
  • sureshkumar
    sureshkumar almost 7 years
    Is it possible to work with TCP ? Or it is only woks for the UDP.
  • mido
    mido almost 7 years
    yes, it is possible to work only with TCP but afraid the performance might be bad
  • Hassaan
    Hassaan almost 6 years
    Thank you for great solution. I am getting this error on mozilla firefox uncaught exception: buildPeerConnection failed, call not completed I am using easyRTC. Can you help me?
  • Khalil Laleh
    Khalil Laleh over 5 years
    To skip IPv6 issue, enter the server IP. This worked for me like a charm: turnserver -L <Your IP> -a -o -f -n --no-dtls --no-tls -u username:password -r yourdomain.com
  • Mushfiqur Rahman
    Mushfiqur Rahman over 5 years
    I followed this process in one of my EC2 instance and found the TURN server is working fine. But in another EC2 instance I followed these steps too but it's not working when testing through webrtc.github.io/samples/src/content/peerconnection/trickle-‌​ice Here I should mention that both EC2 is running on Ubuntu 16.04 and all the incoming and outgoing ports or configuration are same. Not getting any clue. Can anyone suggest how can I figure out the issue? I tried by uninstall and reinstall but nothing working.
  • Hassaan
    Hassaan about 5 years
    I want to enable support SSL certificates with this command. Can you please help me with this?
  • Ali Bahrami
    Ali Bahrami almost 5 years
    That's a legacy now
  • Kira
    Kira about 4 years
    Worked for me, thanks :-). Side note, in case your server shows as unreachable, check if your server firewall is blocking the port
  • ishan shah
    ishan shah about 4 years
    It returns 401 Unauthorised error still, it works! Any idea?
  • songz
    songz over 3 years
    Needs to be updated, did not work on firefox. TurnConfig now takes urls arary instead of url: urls: [turn:${IP}:${PORT}?transport=tcp]