Twilio's Network Traversal Service is a globally distributed STUN/TURN service that helps you deploy more reliable peer-to-peer communications applications.
You can use this service in your WebRTC and VoIP applications for traversal and relay around NAT/firewalls, so that your users make a successful connection every time.
If you're new to STUN/TURN and ICE, check out the Frequently Asked Questions. If you want to start using Twilio's Network Traversal Service immediately, here's what you need to do.
Using Network Traversal Service in a WebRTC application is as easy as requesting a token and passing it to your RTCPeerConnection constructor.
First, make a request from your web server to retrieve a Network Traversal Service Token and then pass it to your WebRTC application.
You'll need to use your Twilio Account SID and Auth Token to get a Network Traversal Service Token. To keep your Twilio account credentials safe, you should only make this request from your server, not the client browser.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createToken() {11const token = await client.tokens.create();1213console.log(token.accountSid);14}1516createToken();
1{2"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",3"ice_servers": [4{5"urls": "stun:global.stun.twilio.com:3478"6},7{8"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",9"credential": "tE2DajzSJwnsSbc123",10"urls": "turn:global.turn.twilio.com:3478?transport=udp"11},12{13"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",14"credential": "tE2DajzSJwnsSbc123",15"urls": "turn:global.turn.twilio.com:3478?transport=tcp"16},17{18"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",19"credential": "tE2DajzSJwnsSbc123",20"urls": "turn:global.turn.twilio.com:443?transport=tcp"21}22],23"date_updated": "Fri, 01 May 2020 01:42:57 +0000",24"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",25"ttl": "86400",26"date_created": "Fri, 01 May 2020 01:42:57 +0000",27"password": "tE2DajzSJwnsSbc123"28}
Next, include the ice_servers
property in the RTCConfiguration object you pass to the RTCPeerConnection constructor when setting up a call, as shown below. Here, ICE_SERVERS
contains the contents of the ice_servers
property returned in the response above:
1// Here's an example in javaScript2myIceServers = ICE_SERVERS;3var configuration = { iceServers: myIceServers };4var pc = new RTCPeerConnection(configuration);
From this point, exchange SDP (Session Description Protocol) offer/answers as you normally would.
Well done! You've now used Network Traversal Service to set up a connection.
If the browser you're using supports Trickle ICE, you'll also want to handle the RTCPeerConnection's onIceCandidate
event to pass any new ICE candidates to the connected peer:
1// Here's an example in javaScript2pc.onicecandidate = function (evt) {3if (evt.candidate) {4// Send the candidate to the other party via your signaling channel5}6};
That's all there is to it! If you need more help, check out the Frequently Asked Questions. Still did not find an answer to your question? You can contact Twilio Support through the Console or Help Center.
In order to use Network Traversal Service in a VoIP client, you'll need to be able to make an HTTP request from your VoIP client or another application at least once every 24 hours to retrieve a new ephemeral token.
To use Network Traversal Service with a VoIP Client, follow these steps:
Network Traversal Service Tokens are good for up to one day, and that's their default value. You can lower the token expiration period by setting the Ttl
parameter when you request the token. Find out more in the Network Traversal Service Token documentation.