Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

How to Detect TURN Server Usage in WebRTC

In WebRTC, connecting two peers involves finding the best path using Interactive Connectivity Establishment (ICE). This path can be direct (peer-to-peer) or relayed via a TURN server. Knowing which path your connection uses is important for monitoring performance and managing costs.

Your connection type—direct or relayed—is determined by the selected ICE candidate.

The Three Types of ICE Candidates

ICE candidates are the network addresses your browser discovers to reach a remote peer:

Candidate Type Description Connection Path
host Direct local IP (LAN) Direct (Local Network)
srflx Public IP discovered via STUN Direct (Internet P2P)
relay Routed through a TURN server Relayed (TURN Server)

Tip: If the selected candidate type is relay, your connection is definitely using a TURN server

Step 1: Listen for ICE Candidates

You can track all discovered candidates by listening for the icecandidate event on your RTCPeerConnection:

peerConnection.addEventListener("icecandidate", event => {
       if (event.candidate) {
             console.log("ICE candidate:", event.candidate.candidate);
           // Look for "typ relay" to detect TURN usage
       }
});

Step 2: Check the Selected Candidate Pair Using getStats()

The most reliable method is using the getStats() API. This reports the candidate pair that has been selected and successfully connected.

async function checkTurnUsage(peerConnection) {
       const stats = await peerConnection.getStats();

       stats.forEach(report => {
             if (report.type === "candidate-pair" && report.state === "succeeded" && report.selected) {
                   const local = stats.get(report.localCandidateId);
                   const remote = stats.get(report.remoteCandidateId);

                   if (local?.candidateType === "relay" || remote?.candidateType === "relay") {
                         console.log("Connection is using TURN (relay).");
                   } else {
                         console.log("Connection is direct (host/srflx).");
                   }
             }
       });
}

Step 3: Continuously Monitor Path Changes

WebRTC connections can switch ICE candidates if network conditions change. To monitor dynamically, listen for relevant events:

// When the selected candidate pair changes
peerConnection.addEventListener("icecandidatepairchange", () => {
       checkTurnUsage(peerConnection);
});

// When the connection becomes stable
peerConnection.addEventListener("iceconnectionstatechange", () => {
       if (peerConnection.iceConnectionState === "connected" ||
            peerConnection.iceConnectionState === "completed") {
             checkTurnUsage(peerConnection);
       }
});

Summary Checklist

Action Purpose Key Indicator
Check ICE Candidate Type Identify potential paths host → local / srflx → direct P2P / relay → TURN
Use getStats() Confirm selected pair Look for candidateType: "relay"
Monitor Events Track dynamic changes icecandidatepairchange or iceconnectionstatechange

 

Conclusion

Detecting TURN server usage is crucial for optimizing WebRTC performance and controlling costs. By understanding host, srflx, and relay candidates, using getStats() to verify the selected pair, and monitoring events for changes, developers can ensure reliable, real-time connectivity. This approach helps deliver smooth, high-quality WebRTC experiences while keeping infrastructure usage efficient.

Ready to get started?

Contact IVC for a free consultation and discover how we can help your business grow online.

Contact IVC for a Free Consultation
Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US