webrtcstunturnvideochatsimple-peer

How to setup STUN server in a video chat app built using simple peer?


I was recently working on a project which requires video chatting. I used simple peer to setup a video call and use socket.io for signalling purposes. I then deployed my application. I realised when two peers on the same network join the call, the app works fine. But if two peers on different networks join the call, then I got an error stating process not defined and the call does not connects. I read about this online and then figured out that I also have two configure a STUN and/or TURN server to extract ICE candidates and their public IP.

Can anyone please tell me how to setup a STUN server in my simple peer application? I have also read somewhere that google provides some free STUN servers to use but I dont know how to actually integrate them in my simple peer application.


Solution

  • When you create the RTCPeerConnection in your application, provide a configuration that includes iceServers.

    This is the reference.

    Example:

    myPeerConnection = new RTCPeerConnection({
      iceServers: [
        {
          urls: "stun:stunserver.example.org"
        }
      ]
    });
    

    You can find a list of free STUN servers here.

    You may also want to configure TURN servers to cover more complex NAT scenarios.