Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the differences between STUN and TURN servers in WebRTC
    STUN server TURN server WebRTC NAT traversal peer-to-peer connections relay traffic WebRTC connection setup


What are the differences between STUN and TURN servers in WebRTC


STUN (Session Traversal Utilities for NAT) and TURN (Traversal Using Relays around NAT) servers are both used in WebRTC to help establish and maintain peer-to-peer connections across networks. Here are the key differences between them:

STUN Server

1. Purpose: STUN servers help devices behind a NAT (Network Address Translation) to discover their public IP address and the type of NAT they are behind.
2. Functionality: STUN servers allow clients to determine if they can establish a direct connection with the peer. If the direct connection fails, the client can use a TURN server to relay traffic.
3. Usage: STUN servers are typically used during the connection setup phase to determine if a direct connection can be established. If the direct connection fails, the client will use a TURN server to relay traffic.

TURN Server

1. Purpose: TURN servers are used to relay traffic around restrictive networks. They help establish a relayed connection between two peers when a direct connection cannot be established.
2. Functionality: TURN servers act as a relay server, allowing traffic to pass through them to reach the target endpoint. They are used when a direct connection cannot be established due to restrictive firewall rules or NATs.
3. Usage: TURN servers are used as a fallback option when a direct connection cannot be established. They are typically used in environments where UDP traffic is blocked or restricted.

Key Differences

1. Direct Connection vs. Relayed Connection: STUN servers help establish direct connections, while TURN servers are used to relay traffic when direct connections fail.
2. Purpose: STUN servers are used for discovery and direct connection establishment, while TURN servers are used for relaying traffic around restrictive networks.
3. Usage: STUN servers are used during the connection setup phase, while TURN servers are used as a fallback option when direct connections fail.

Example Code

Here is an example of how you can use both STUN and TURN servers in your WebRTC application:

javascript
var myPeerConnection = new RTCPeerConnection({
  iceServers: [
    {
      urls: "stun:stun.l.google.com:19302",
    },
    {
      urls: "turn:relay.metered.ca",
      username: "your-turn-server-username",
      credential: "your-turn-server-password",
    },
  ],
});

Conclusion

In summary, STUN servers are used for direct connection establishment and discovery, while TURN servers are used for relaying traffic around restrictive networks. Both are essential components of WebRTC applications to ensure reliable and seamless communication across different network environments.
Citations:
[1] https://blog.ivrpowers.com/post/technologies/what-is-stun-turn-server/
[2] https://www.metered.ca/stun-turn
[3] https://github.com/react-native-webrtc/react-native-webrtc/blob/master/Documentation/ImprovingCallReliability.md
[4] https://developer.liveswitch.io/liveswitch-server/guides/what-are-stun-turn-and-ice.html
[5] https://www.videosdk.live/developer-hub/stun-turn-server/webrtc-turn-server