twiliowebrtcpeerjs

How to use peerJS with twilio STUN/TURN servers


I have a webRTC application that works fine with the default peerJS settings for a STUN server. I now want to switch to STUN/TURN servers from twilio. I have obtained the iceServers structure from twilio (updated daily). Typically, the iceServer stucture looks like:-

var iceArray = [{ "Credential": null, "Username": null, "Url": "stun:global.stun.twilio.com:3478?transport=udp", "Urls": "stun:global.stun.twilio.com:3478?transport=udp" }, ...];

I create a Peer with peerJS using:-

peer = new Peer(myId, { "config" : { "iceServers" : iceArray }});

This does not seem to make an attempt to use the Twilio servers, looking at chrome://webrtc-internals/ shows peerJS is still using the default google server.


Solution

  • Twilio developer evangelist here.

    I think you have everything right here, except perhaps the capitalisation in the iceArray. WebRTC expects the ICE servers config to have lowercase keys.

    Twilio does actually return the ICE servers in lower case format (see the example here) but some of the Twilio helper libraries transform API results into idiomatic formats based on the language, sometimes capitalising keys. If you are getting that result from the API request you made with a Twilio helper library, you might want to look into turning the keys lowercase again, or getting the raw result from the library.