Simple-peer: Works even if the TURN server credentials are wrong

Created on 24 May 2018  路  13Comments  路  Source: feross/simple-peer

The following code logs the offer text but shouldn't it give an error because the turn server doesn't exist?
Is there a fallback server which the app connects to if the turn server is not found?

const wrtc = require('wrtc');
const Peer = require('simple-peer');

const turnServer = {
    urls: 'turn:fake.address:3478',
    username: 'u',
    credential: 'c'
}
const peer1 = new Peer({ 
    initiator: true, 
    wrtc: wrtc, 
    config: { iceServers: [ turnServer ] }
})

peer1.on('signal', function (data) {
    console.log(JSON.stringify(data));
})
question

All 13 comments

If the TURN server isn't found, the peer will create and offer and connect like normal for most networks. TURN servers are optional relay servers for when a P2P connection can't be made due to network conditions.

The TURN server being unreachable is a pretty common occurrence (whether the server actually exists or not.)

If you force the peer to ONLY use TURN with new Peer({config: {iceTransportPolicy: 'relay'}}), you'll get an IceFailure error.

There is no fallback TURN server in simple-peer.

I have tried adding
iceTransportPolicy: 'relay'

but it still generates the offer text but the offer text is a little bit shorter, 3 lines of text instead of 5
but it means its still working, right?

you said:

If the TURN server isn't found, the peer will create and offer and connect like normal for most networks.

what do you mean by normal? like by using STUN server?
so what if I add a fake STUN server too?

const wrtc = require('wrtc');
const Peer = require('simple-peer');

const stunServer = {
    urls: 'stun:fake.address:3478'
}
const turnServer = {
    urls: 'turn:fake.address:3478',
    username: 'u',
    credential: 'c'
}
const peer1 = new Peer({ 
    initiator: true,
    wrtc: wrtc,
    config: { iceServers: [ stunServer, turnServer ] }
})

peer1.on('signal', function (data) {
    console.log(JSON.stringify(data));
})

No error in this either :(

The offer should be shorter and generate without an error. You'd get a ice failure during connection, unless I've made some mistake about the behaviour iceTransportPolicy: 'relay' .

Yes, normal as in using STUN. You can add your own STUN server. You shouldn't get an error in that code either.

What kind of error would you expect to be thrown? When the peer creates an offer, it doesn't attempt to connect to your TURN server, it just puts the information you give it into a offer object.

Connection failures happen when peers try to use that offer to connect.

in the applications of STUN server they say "The STUN server allows clients to find out their public address" so isn't that server necessary to get proper details in the form of offer. I mean isnt the correct server needed to generate the offer? else the offers will be generated on client side without these servers.

The offer is generated client side. The client takes the information you give it and puts it into a format (SDP) to send over the signalling channel. It doesn't connect to any servers during this process*.

Once signalling is complete, the peers will connect directly if they are on the same local network, try to use STUN if they're not, and then use TURN if STUN fails. So if your TURN server doesn't exist and STUN works, the peers will never even notice. Likewise, a bad STUN server won't be contacted if the peers are on the same local network or machine.

*Aside from ICE candidate gathering without trickle, but if the server is unreachable, it will just be skipped.

wow man. thanks for that explanation. now the concept of STUN and TURN is so much clear in my head.

and this - https://stackoverflow.com/a/34033938/9808323 might help me in finding if the TURN server is legit or not

If you're only trying to debug STUN or TURN servers, here's a useful tool:

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

yeah thanks

Looks good to me. You can disable trickle ice if you want all candidates in the offer right away. Good luck.

umm, what do you mean by all candidates?

no nevermind, got it

Was this page helpful?
0 / 5 - 0 ratings