Search code examples
webrtcpeersimplewebrtc

Why my WebRTC code is working without specifying STUN/TURN server url?


I have been able to connect peer to peer device using webrtc. The question in my mind is that the RTCPeerConnection()- here we pass the stun/turn urls. But it is also working when nothing is passed !

I would like to know is it using google stun server internally and if yes why is it not mentioned in any of their documentations.

peerConnection = new RTCPeerConnection();
    peerConnection.setRemoteDescription(description)
    .then(() => peerConnection.createAnswer())
    .then(sdp => peerConnection.setLocalDescription(sdp))
    .then(function () {
        socket.emit('answer', id, peerConnection.localDescription);
    });
    peerConnection.ontrack = function(event) {
        video.srcObject = event.streams[0];
    };
    peerConnection.onicecandidate = function(event) {
        if (event.candidate) {
            socket.emit('candidate', id, event.candidate);
        }

Solution

  • 1) You don't need STUN server in local network.

    2) STUN server is used by device to access own public address. (As both device are on differnet network they can't connect to each other because they don't know each other public IP address. If they know each other public IP address, they can establish P2P connection)

    3) TURN server is used as backup server. (When UDP hole punching dosen't work). TURN server traverse whole data because P2P connection cannot be established.

    If your testing on local network then there is no need of STUN server.

    To use STUN server you do need to specify IP address.