You're viewing the documentation for the 2.X version of the Voice JavaScript SDK. View the Migration Guide to learn how to migrate from 1.X to 2.X or view the 1.x-specific documentation.
Twilio Voice SDKs allow you to build high-quality calling experiences directly into web and mobile applications. They can be used to build use cases like contact centers, sales dialers, peer-to-peer calling and more using familiar web and mobile development tools.
There are a few things you need to keep in mind to get the most out of the Voice JavaScript SDK. Following these best practices will ensure your users have a seamless calling experience. They will also make it easier to troubleshoot connection and call quality issues.
To get the most out of this guide, use it in conjunction with the Voice JS SDK quickstarts and documentation.
The JavaScript SDK exposes a loglevel based logger to allow for runtime logging configuration.
To configure the log level, use the logLevel
property in the DeviceOptions object when instantiating a Twilio.Device
or when calling device.updateOptions()
on a Twilio.Device
instance.
The DeviceOptions.logLevel
value is a number that corresponds to the different levels of logging available:
DeviceOptions.logLevel value | Logging Level |
---|---|
logLevel: 0 | "TRACE" |
logLevel: 1 | "DEBUG" |
logLevel: 2 | "INFO" |
logLevel: 3 | "WARN" |
logLevel: 4 | "ERROR" |
logLevel: 5 | "SILENT" |
Below are examples for how to enable "DEBUG" level logging when instantiating a Twilio.Device
and when calling device.updateOptions()
.
1// when instantiating a Twilio.Device23const device = new Twilio.Device (token, { logLevel: 1 });456// when calling device.updateOptions()78const deviceOptions = {9logLevel: 110}1112device.updateOptions(deviceOptions);
The JavaScript SDK relies on events following the EventEmitter interface to control the calling experience. Alerting the user to an incoming call requires listening for the Device.on('incoming')
event, for example. Similarly, the SDK also provides events for monitoring the Device
and Call
states.
Surfacing changes in the Device
and Call
states to the UI using these events can often be the difference between a smooth calling experience and an extremely frustrating one. Important events to listen for include the following:
The Device.on('registered')
event is fired once the Device has been successfully setup using a valid access token and is registered to receive incoming calls (note that the voice client can still make outbound calls if it is not registered; see more information here). Use this event to change a UI element, like a status indicator for example. This ensures the user is aware that your application is online and ready to start making and receiving calls.
Similarly, it's important to notify the user if your application goes offline at any point of time. Use the Device.on('unregistered') event to change the status indicator to offline to alert the user. This event is triggered if the connection to Twilio drops for some reason or if the access token expires. You should also use this event to attempt to reconnect using Device.register()
.
Handling this event allows you to catch and handle device errors gracefully. You can see the list of errors surfaced by this handler here. Some commonly encountered errors are:
It's also important to gracefully handle situations where a call to the voice client goes unanswered despite it being online. How to handle this depends on how the voice client is brought into the call:
Incoming calls can be connected to the voice client using the <Dial>
verb's <Client> noun. In this case, you should set the timeout attribute to a value that works best for your use case. You should also configure an action URL using the action attribute. Twilio will make a request to this URL with these parameters once the call is concluded, and will include information the outcome of the call (whether it was answered or not answered).
You can also use the REST API to bring a voice client into a call. This is involves first placing an outbound call to the client. When the client picks up, the Url parameter retrieves TwiML that is used to set up the call. You can learn more about using the REST API here. It's important to set a Timeout on the API request that works best for your use case. Note that the max is 60 seconds for calls made to the voice client. Be sure to configure a status callback URL using the StatusCallback parameter and specify the call progress event webhooks using the StatusCallbackUrl
parameter. This ensures your application knows the outcome of the call.
If the call outcome in both situations is no-answer, it's important this is conveyed to the caller. One way to do this is by directing them to voicemail. You can use the <Record> verb to set up voicemail. If the call is unanswered, the caller is directed to TwiML that uses the <Record>
verb to leave a voicemail.
The SDK will automatically choose the default input and output devices when placing or receiving calls. However, we recommend asking for device permissions before creating the device object to avoid problems at call connect or accept time e.g. hardware issues or permissions related issues. The following code snippet demonstrates how to do this.
1// Call getUserMedia to ask for device permission and populate the device labels.2// Also, performing this action here allows for capturing gUM (getUserMedia) errors early3// before accepting/receiving a call and it's possible to create a much better user experience.4const stream = await navigator.mediaDevices.getUserMedia({ audio: true });56// Calling getUserMedia will start the media track selected.7// This is not desired as the user may get the impression the mic is in use.8// Therefore, we want to avoid having tracks started when they're not needed.9// We only wanted to get the input device list so we stop the tracks immediately.10stream.getTracks().forEach(track => track.stop());1112// Create device object using your token and desired options.13const device = new Device(token, options);14device.register();1516// Our example UI is a Dropdown that shows the available input devices (microphones).17// The user can select the input device.18const micOptions = document.createElement('select');19micOptions.addEventListener('change', () => {20device.audio.setInputDevice(micOptions.value);21});2223// Update UI with the updated list of available devices.24const updateMicOptions = () => {25micOptions.innerHTML = '';26device.audio.availableInputDevices.forEach(d => {27const option = document.createElement('option');28option.value = d.deviceId;29option.innerText = d.label;30micOptions.appendChild(option);31});32};3334// Populate the dropdown once registered.35device.on('registered', () => updateMicOptions());3637// We want to detect if the device list changes e.g. a headset was plugged in/out.38// We set up handlers to update our dropdown list with the new device list39// Subscribe to the event for when the list of devices changes40device.audio.on('deviceChange', () => updateMicOptions());41
Voice Insights for Twilio Voice SDKs provides call quality analytics for client calls. It provides a REST API for retrieving historical call quality statistics such as jitter, Mean Opinion Socre (MoS) and packet loss. It also provides a component in the JavaScript SDK that fires events when call quality drops below acceptable thresholds. This can be used to notify the user in real time about issues with call quality.
Voice Insights fires two types of events on the front end: network warnings and audio level warnings.
By implementing handlers for these events and surfacing them in the UI, you can notify the user about degradation in call quality or issues with audio input. This can be used to prompt the user to take remedial action like checking their internet connection or input device audio.
Implementing Voice Insights for Twilio Voice SDKs can also make troubleshooting issues a lot easier. The Voice Insights dashboard in the console provides aggregate call quality metrics across all calls, and can be filtered to just voice client calls. This is useful in seeing trends in your call quality stats. For example, you could see that client calls with a particular browser version are seeing more issues with quality. It also records call setup events, allowing you to diagnose issues with call connection. The same data is also made available for individual calls.
You can learn more about Voice Insights by checking out the docs.
VoIP call quality is heavily influenced by environmental factors like firewall configuration, network conditions and available bandwidth, browser version (for webRTC) and OS and microphone and speaker hardware. It's important you review our deployment best practices and connectivity requirements documentation before taking your app to production.
If possible, you should also take advantage of the DSCP support enabled in the Voice JavaScript SDK version 1.3 onwards. DSCP, or Differentiated Services Code Point, allows packets to be tagged to prioritize them on the network. Browsers that support DSCP are capable of tagging call media packets sent by the voice client in this manner. Your router or network element can then use these tags to prioritize call media packets over other traffic on the network. Also, note that your router or network element needs to be DSCP-compliant.
DSCP is currently supported only by Google Chrome. For help setting DSCP on a Windows machine, please see this Zendesk article.
Twilio has a global presence with data centers around the world; see the full list of locations here. Connecting to edge locations minimizes latency by allowing your Twilio client device to connect to the closest point of presence. There are two ways you can set up your Client to connect to Twilio:
edge
parameter while creating the device.edge
parameter. You can find the list of edges and their IP addresses here. This approach makes sense if all Twilio Voice clients are going to be based in the same edge location. You can instantiate the device with the edge
parameter or use device.updateOptions
to set the edge.The Voice JavaScript SDK provides three features to help keep your AccessTokens up to date.
The 'tokenWillExpire' event emitted by the Twilio.Device
instance
DeviceOptions.tokenRefreshMs
property.The DeviceOptions.tokenRefreshMs property
'tokenWillExpire'
event will be emitted.As shown in the example below, you can use these three features together to automatically keep an AccessToken up to date.
1const device = new Device(token, {2// 'tokenWillExpire' event will be emitted 30 seconds before the AccessToken expires3tokenRefreshMs: 30000,4});56device.on('tokenWillExpire', () => {7return getTokenViaAjax().then(token => device.updateToken(token));8});
As of version 2.5.0, the Voice JavaScript SDK allows you to override WebRTC APIs using the following options and events. If your environment supports WebRTC redirection, such as Citrix HDX's WebRTC redirection technologies, your application can use this feature for improved audio quality in those environments.
The following code snippet demonstrates how to use the Voice JavaScript SDK to enable Citrix HDX's WebRTC redirection.
1// Prerequisites:2// - Citrix's UCSDK 3.1.0 or later3// - Twilio Voice JS SDK 2.5.0 or later4// - Proper Citrix VDI environment setup with HDX support567// Before loading the Citrix UCSDK, it is necessary to define a global8// function with the name `getCitrixWebrtcRedir` as required by Citrix's UCSDK.9// The example provided below will always resolve and return `1`,10// indicating the application is always running in a Citrix environment.11// This behavior can be dynamic, and the function should reject if12// redirection is not supported.13// For further details, refer to the Citrix HDX WebRTC Redirection SDK14// Documentation. To access Citrix's SDK and its documentation, please15// contact Citrix directly.16window.getCitrixWebrtcRedir = () => new Promise(res => res(1));1718// Load Citrix's UCSDK using RequireJS.19// As of version 3.1.0, Citrix's UCSDK must be loaded via RequireJS.20// Attempting alternative methods, such as using <script> tags or import statements,21// will lead to errors.22require(['./CitrixWebRTC'], async () => {23// Initialize Citrix's UCSDK24CitrixWebRTC.initUCSDK('twilio-citrix-partner');2526// Citrix connected/disconnected logs.27CitrixWebRTC.setVMEventCallback(event => {28console.log(`Got Citrix VM Event:`, event)29if (event.event === 'vdiClientConnected') {30console.log('Citrix webrtc vdiClientConnected');31} else if ( event.event == 'vdiClientDisconnected') {32console.log('Citrix webrtc disconnected');33}34});3536// Initialize Twilio Device object using your own token.37const device = new Twilio.Device(token, {38// RTCPeerConnection and enumerateDevices needs the UCSDK's scope so we bind them39RTCPeerConnection: CitrixWebRTC.CitrixPeerConnection.bind(CitrixWebRTC),40enumerateDevices: CitrixWebRTC.enumerateDevices.bind(CitrixWebRTC),41// getUserMedia's parameters are needed so we make sure we don't lose them42getUserMedia: (...args) => CitrixWebRTC.getUserMedia(...args),43// ... other device options44});4546let remoteAudio;47const setupOnAudioElement = call => {48// Listen for audio event. Triggers when the audio element49// used for remote audio stream has been created.50call.on('audio', audioElement => {51// Remove any previous mapping52if (remoteAudio) {53CitrixWebRTC.destroyAudioElement(remoteAudio);54remoteAudio = null;55}56// Map the audio element that was created for57// remote audio stream as soon as it's available58CitrixWebRTC.mapAudioElement(audioElement);59remoteAudio = audioElement;60});61};6263// If making outgoing calls64const call = await device.connect({65rtcConfiguration: {66// Needs explicit sdpSemantics and enableDtlsSrtp67sdpSemantics: 'unified-plan',68enableDtlsSrtp: true,69},70// params: ... your other params if necessary71});72setupOnAudioElement(call);7374// For incoming calls75device.on('incoming', call => {76setupOnAudioElement(call);77call.accept({78rtcConfiguration: {79// Needs explicit sdpSemantics and enableDtlsSrtp80sdpSemantics: 'unified-plan',81enableDtlsSrtp: true,82}83});84});85});86