TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: How to test whether a STUN/TURN server is working properly

17 pointsby mp85almost 3 years ago
A few days ago, I created a simple service for my WebRTC Project: <a href="https:&#x2F;&#x2F;github.com&#x2F;miroslavpejic85&#x2F;mirotalk" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;miroslavpejic85&#x2F;mirotalk</a> to test if the STUN&#x2F;TURN servers are working properly.<p>After installing and configuring a STUN&#x2F;TURN server, most of the developers that are new to this stuff will ask themselves, how do I know if it&#x27;s working properly?<p>So I made a snippet :)<p>```<p>&lt;!DOCTYPE html&gt;<p>&lt;html&gt;<p>&lt;head&gt; &lt;title&gt;Test Stun&#x2F;Turn Servers&lt;&#x2F;title&gt;<p><pre><code> &lt;meta charset=&quot;utf-8&quot; &#x2F;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; &#x2F;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; &#x2F;&gt;</code></pre> &lt;&#x2F;head&gt;<p>&lt;body&gt; &lt;h1&gt;Test Ice Servers&lt;&#x2F;h1&gt;<p><pre><code> &lt;hr &#x2F;&gt; &lt;pre id=&#x27;ice&#x27; style=&#x27;overflow: auto&#x27;&gt;&lt;&#x2F;pre&gt; &lt;hr &#x2F;&gt; &lt;p id=&#x27;ip&#x27;&gt;&lt;&#x2F;p&gt; &lt;p id=&#x27;stun&#x27;&gt; The STUN server is NOT reachable!&lt;&#x2F;p&gt; &lt;p id=&#x27;turn&#x27;&gt; The TURN server is NOT reachable!&lt;&#x2F;p&gt; &lt;p id=&#x27;err&#x27;&gt;&lt;&#x2F;p&gt; &lt;hr &#x2F;&gt; &lt;script&gt; const Ice = document.getElementById(&#x27;ice&#x27;); const IP = document.getElementById(&#x27;ip&#x27;); const Stun = document.getElementById(&#x27;stun&#x27;); const Turn = document.getElementById(&#x27;turn&#x27;); const Err = document.getElementById(&#x27;err&#x27;); const iceServers = [ &#x2F;&#x2F; Test some STUN server { urls: &#x27;stun:stun.l.google.com:19302&#x27;, }, &#x2F;&#x2F; Test some TURN server { urls: &#x27;turn:turnUrl&#x27;, username: &#x27;turnUsername&#x27;, credential: &#x27;turnPassword&#x27;, }, ]; &#x2F;&#x2F; Print iceServers config Ice.innerHTML = JSON.stringify(iceServers, null, 4); &#x2F;&#x2F; Test the connections const pc = new RTCPeerConnection({ iceServers }); pc.onicecandidate = (e) =&gt; { if (!e.candidate) return; console.log(e.candidate.candidate); &#x2F;&#x2F; If a srflx candidate was found, notify that the STUN server works! if (e.candidate.type == &#x27;srflx&#x27; || e.candidate.candidate.includes(&#x27;srflx&#x27;)) { let ip = &#x2F;\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b&#x2F;; let address = e.candidate.address ? e.candidate.address : e.candidate.candidate.match(ip); IP.innerHTML = &#x27; Your Public IP Address is &#x27; + address; Stun.innerHTML = &#x27; The STUN server is reachable!&#x27;; } &#x2F;&#x2F; If a relay candidate was found, notify that the TURN server works! if (e.candidate.type == &#x27;relay&#x27; || e.candidate.candidate.includes(&#x27;relay&#x27;)) { Turn.innerHTML = &#x27; The TURN server is reachable!&#x27;; } }; &#x2F;&#x2F; handle error pc.onicecandidateerror = (e) =&gt; { console.error(e); Err.innerHTML = &#x27; Error: &#x27; + e.errorText; }; pc.createDataChannel(&#x27;test&#x27;); pc.createOffer().then(offer =&gt; pc.setLocalDescription(offer)); &lt;&#x2F;script&gt; </code></pre> &lt;&#x2F;body&gt;<p>&lt;&#x2F;html&gt;<p>```<p>Alternative to <a href="https:&#x2F;&#x2F;webrtc.github.io&#x2F;samples&#x2F;src&#x2F;content&#x2F;peerconnection&#x2F;trickle-ice&#x2F;" rel="nofollow">https:&#x2F;&#x2F;webrtc.github.io&#x2F;samples&#x2F;src&#x2F;content&#x2F;peerconnection&#x2F;...</a><p>Ref: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;selfhosted&#x2F;comments&#x2F;wayiev&#x2F;how_to_test_whether_a_stunturn_server_is_working&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;selfhosted&#x2F;comments&#x2F;wayiev&#x2F;how_to_t...</a><p>I hope it will be useful to someone.

no comments

no comments