Learn how to check if your recently deployed STUN/TURN server works properly or is at least reachable.

In the last few days, I had to work once again with WebRTC launching a new project that uses Janus in a server. One of the components that this tool requires to work properly everywhere is to have a STUN server to allow clients to find out their public address, the type of NAT they are behind, and the Internet side port associated with the NAT with a particular local port.

Someway, the deploying of a STUN server is "easy", however after installing and configuring it, most of the developers that are new to this stuff will ask themselves, how do I know if it's working properly? I mean, yeah the service in the server is up and running it seems:

Coturn Service Up and Running Ubuntu Server

But, is it reachable to whoever needs access to it? In this article, I will explain to you how to easily test if your STUN/TURN server is reachable.

Rule of thumb

You can easily determine if your server works with both tools or with your own JavaScript:

  • A STUN server works if you can gather a candidate with type "srflx".
  • A TURN server works if you can gather a candidate with type "relay".

That's all you should need to know if your server is working using the tools that we'll mention in this article.

A. Using TrickleICE

The first tool that you can use to test if your STUN/TURN server is working, is the Trickle ICE tool from the official samples of the WebRTC repository at Github. You can test the online tool on this website:

Trickle ICE

All that you need to do is to add the STUN or TURN URI that follows the following pattern protocol:domain:port. Then, click on Gather candidates and wait for the application to run the test. In this case, we provided a single STUN server, so we expect a srflx candidate type to show up, which would confirm that the server is working properly!

In case that you are testing a TURN server, you need to expect a relay candidate type to show up to confirm that is working successfully:

Relay Candidate TURN Server

B. Using ICE Test

The other alternative to Trickle ICE is the ICE Test website that you can visit here (or the source code at Github here). It works basically in the same way, you can create your own list of ICE servers providing the STUN/TURN URI and credentials if necessary to test them:

ICE Server Test

C. Using your own JavaScript

Alternatively, you can write your own JavaScript to do the basic try to gather the candidates and check if was possible to gather srflx or relay candidates:

const iceServers = [
    // Test some STUN server
    {
        urls: 'stun:mydomain.com:port?transport=udp'
    },
    // Test some TURN server
    {
        urls: 'turn:mydomain.com:port?transport=udp', 
        username: 'username', 
        credential: 'password'
    }
];

const pc = new RTCPeerConnection({
	iceServers
});

pc.onicecandidate = (e) => {
    if (!e.candidate) return;

    // Display candidate string e.g
    // candidate:842163049 1 udp 1677729535 XXX.XXX.XX.XXXX 58481 typ srflx raddr 0.0.0.0 rport 0 generation 0 ufrag sXP5 network-cost 999
    console.log(e.candidate.candidate);

    // If a srflx candidate was found, notify that the STUN server works!
    if(e.candidate.type == "srflx"){
        console.log("The STUN server is reachable!");
        console.log(`   Your Public IP Address is: ${e.candidate.address}`);
    }

    // If a relay candidate was found, notify that the TURN server works!
    if(e.candidate.type == "relay"){
        console.log("The TURN server is reachable !");
    }
};

// Log errors:
// Remember that in most of the cases, even if its working, you will find a STUN host lookup received error
// Chrome tried to look up the IPv6 DNS record for server and got an error in that process. However, it may still be accessible through the IPv4 address
pc.onicecandidateerror = (e) => {
    console.error(e);
};

pc.createDataChannel('ourcodeworld-rocks');
pc.createOffer().then(offer => pc.setLocalDescription(offer));

Happy coding ❤️!


Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person

Sponsors