I am working on a very basic WebRTC project, but I can't seem to get my website to connect to my TURN server. I'v setup the TURN server using coturn on my local machine (MacOS 10.15.4), and I'm pretty sure its working and configured properly. (when I test it from https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/ I can see an rtp host, rtp srflx and an rtp relay, both from inside and outside of my local network, so I'm assuming that means its working as its supposed to.)
The website is a slightly modified version of the this codelab's step 5 example, with the node.js index.js file modified slightly to host the site over https to allow webcam access from outside the local network.
Everything works fine as long as both clients are on the same network (so no TURN server is needed), but as soon as one of the clients tries to connect from a different network then the other client, the PeerConnection doesn't gets established properly and both clients can only see their own localStream.
I think something is missing from the JavaScript on the website, because when I test the TURN server from the webrtc.github.io example, I see this output from my TURN server in my terminal:
4271: session 005000000000000002: realm <test> user <>: incoming packet BINDING processed, success
4271: session 005000000000000002: realm <test> user <>: incoming packet message processed, error 401: Unauthorized
4271: IPv4. Local relay addr: 192.168.xxx.xxx:61944
4271: session 005000000000000002: new, realm=<test>, username=<test>, lifetime=600
4271: session 005000000000000002: realm <test> user <test>: incoming packet ALLOCATE processed, success
4271: session 005000000000000002: refreshed, realm=<test>, username=<test>, lifetime=0
4271: session 005000000000000002: realm <test> user <test>: incoming packet REFRESH processed, success
4272: session 005000000000000002: usage: realm=<test>, username=<test>, rp=4, rb=224, sp=4, sb=380
4272: session 005000000000000002: peer usage: realm=<test>, username=<test>, rp=0, rb=0, sp=0, sb=0
4272: session 005000000000000002: closed (2nd stage), user <test> realm <test> origin <>, local 192.168.xxx.xxx:3478, remote 192.168.xxx.xxx:62569, reason: allocation timeout
4272: session 005000000000000002: delete: realm=<test>, username=<test>
(obviously the xxx's are actual numbers in the real output)
When two client on different networks try to load the page and initiate a peer connection however, I get no output from coturn at all in my terminal, so I'm guessing something in my website's JavaScript is missing and it's not using my TURN server. This is all the code related to the TURN server currently on the website:
var pcConfig = {
'iceServers': [
{
'urls': 'stun:stun.l.google.com:19302'
},
{
'urls': 'turn:xxx.xxx.xxx.xxx:3478', <-- (my external IP address)
'username': 'test',
'password': 'test',
}
]
};
if (location.hostname !== 'localhost') {
console.log("Requesting TURN server...");
requestTurn();
}
function requestTurn() {
var turnExists = false;
for (var i in pcConfig.iceServers) {
if (pcConfig.iceServers[i].urls.substr(0, 5) === 'turn:') {
turnExists = true;
turnReady = true;
console.log("Turn ready at: " + pcConfig.iceServers[i].urls);
break;
}
}
if (!turnExists) {
console.log("No TURN server found");
}
}
In the console I see the messages Requesting TURN server...
and Turn ready at: turn:xxx.xxx.xxx.xxx:3478
But since I'm not getting any output from my TURN server in my terminal, I'm guessing the website isn't actually making a request to the TURN server at all. I'm probably missing something obvious in my client side JavaScript, but I have no idea what, so I hope someone has some ideas and can help!
So... It turns out it was a stupid question.
After debugging for longer then I would care to admit, it turns out that the pcConfig where I defined my TURN server was not actually used when creating a new RTCPeerConnection.
All I had to do was change:
pc = new RTCPeerConnection(null);
To:
pc = new RTCPeerConnection(pcConfig);
And now everything works.