Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Network Traversal Service


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

list-post-example page anchor

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.

(warning)

Warning

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.

Generate NTS Token

generate-nts-token page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.tokens.create().then(token => console.log(token.username));

Output

_28
{
_28
"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
_28
"ice_servers": [
_28
{
_28
"urls": "stun:global.stun.twilio.com:3478"
_28
},
_28
{
_28
"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
_28
"credential": "tE2DajzSJwnsSbc123",
_28
"urls": "turn:global.turn.twilio.com:3478?transport=udp"
_28
},
_28
{
_28
"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
_28
"credential": "tE2DajzSJwnsSbc123",
_28
"urls": "turn:global.turn.twilio.com:3478?transport=tcp"
_28
},
_28
{
_28
"username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
_28
"credential": "tE2DajzSJwnsSbc123",
_28
"urls": "turn:global.turn.twilio.com:443?transport=tcp"
_28
}
_28
],
_28
"date_updated": "Fri, 01 May 2020 01:42:57 +0000",
_28
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_28
"ttl": "86400",
_28
"date_created": "Fri, 01 May 2020 01:42:57 +0000",
_28
"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:


_10
// Here's an example in javaScript
_10
myIceServers = ICE_SERVERS;
_10
var configuration = { iceServers: myIceServers };
_10
var 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(link takes you to an external page), you'll also want to handle the RTCPeerConnection's onIceCandidate event to pass any new ICE candidates to the connected peer:


_10
// Here's an example in javaScript
_10
pc.onicecandidate = function (evt) {
_10
if (evt.candidate) {
_10
// Send the candidate to the other party via your signaling channel
_10
}
_10
};

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(link takes you to an external page) or Help Center(link takes you to an external page).


Using Network Traversal Service in a VoIP client

voip-client page anchor
(warning)

Warning

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:

  1. Retrieve a Network Traversal Service Token.
  2. Extract the STUN and TURN URLs from the returned data, along with the username and credential values.
  3. Configure your VoIP client with these values.

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.


Rate this page: