STUN/TURN Servers
STUN (Session Traversal Utilities for NAT) and TURN (Traversal Using Relays around NAT) servers are used to establish peer-to-peer (P2P) connections between clients that are behind NATs (Network Address Translators) or firewalls.
These types of servers are commonly used in WebRTC.
Feature | STUN | TURN |
---|---|---|
Purpose | Discover public IP and port | Relay media when P2P fails |
Operation | exchanges IP/port | Relays full media traffic |
Use Case | Works for most NAT types | For restrictive NATs/firewalls |
Network Load | Low | High (relays media data) |
Reliability | Limited by NAT type | Works when STUN fails |
STUN (Session Traversal Utilities for NAT)
Functionality:
- STUN discovers a client's public IP + port when they are behind a NAT
- Lightweight, efficient and works well for most NAT types
- May struggle with more restrictive types, such as symmetric NATs
Example of how STUN works:
- WebRTC client (e.g. browser) initiates a P2P connection.
- Sends a request to a STUN server
- STUN server responds with client's public-facing IP address and port
- Information is shared with peer during WebRTC's signaling process
TURN (Traversal Using Relays around NAT)
Functionality:
- TURN is more complex and is used as a fallback for direct P2P connections
- Relays media traffic between clients (acting as intermediary for the data)
How TURN works:
- Clients unable to connect directly (using STUN)
- TURN server comes in and acts as relay (receives media data and forwards it)