webrtcturn

In WebRTC, is it required for participants to be connected to the same TURN server?


In WebRTC, when the number of TURN servers have to be increased (horizontal scaling) due to increase in traffic, there seems to be two approaches to tackle this.

  1. The first way is to simply have multiple TURN servers and expose them directly to the internet, and provide a full list of them explicitly to clients. Although, I heard that this can slow down discovery process, this seems to be one possible approach.
  2. The second way is to have multiple TURN servers behind load balancer/1 IP, and provide 1 address to the client. In this case, load balancer will do the work of distributing requests to different turn servers.

In either approach I take, there is no guarantee that all participants in the same room/group will be connected to the same TURN server. I was wondering if this is okay? Is communication between TURN and clients stateless such that peers can be connected to any one of the available TURN servers, and relay of data can still happen?


Solution

  • TURN simply opens a (udp) port on the TURN server. The TURN server will unwrap the STUN packets the client sends it (either STUN send indications or channels) and send them to the remote end. The remote end may be another TURN server but the TURN server does not care -- nor does the client. In terms of protocols used this looks like this:

    Client --(STUN)-->TURN Server --("raw" udp)-->Another TURN Server--(STUN)-->Second client
    Client --(STUN)-->TURN Server --("raw" udp)-->Second client
    

    Note that both clients might be using TURN over TCP to talk to their respective TURN server but the allocations typically happen over udp (in webrtc).

    The first approach is more common, it works better in cases where participants are assigned to the closest TURN server location which may be different if the participants are from different continents.