This guide provides recommendations and best practices for building a Video Application using twilio-video.js.
This table shows the browsers and platforms that are supported by twilio-video.js. Please use the isSupported flag to find out if twilio-video.js supports the browser in which your application is running.
1const { isSupported } = require('twilio-video');2if (isSupported) {3// Set up your video app.4} else {5console.error('This browser is not supported by twilio-video.js.');6}
Please take a look at this guide to choose the right ConnectOptions values for your use case.
twilio-video.js relies on getUserMedia to acquire local media. In order for this API to be available, please ensure that your application is running either on localhost or an https domain.
The autoplay policy does not allow you to autoplay audio using unmuted <audio>
or <video>
elements unless the user has interacted with your application (clicking on a button, for example), especially if your application's media engagement score is not high enough. Please refer to "Working around the browsers' autoplay policy" in the JavaScript SDK's COMMON_ISSUES.md to work around different browsers' autoplay policies.
In mobile browsers, the camera can be reserved by only one LocalVideoTrack at any given time. If you attempt to create a second LocalVideoTrack, video frames will no longer be supplied to the first LocalVideoTrack. So, we recommend that:
If you want to display your camera preview, pre-acquire media using createLocalTracks. You can then pass these LocalTracks to connect.
1const { createLocalTracks, connect } = require('twilio-video');23const tracks = await createLocalTracks();45// Display camera preview.6const localVideoTrack = tracks.find(track => track.kind === 'video');7divContainer.appendChild(localVideoTrack.attach());89// Join the Room with the pre-acquired LocalTracks.10const room = await connect('token', {11name: 'my-cool-room',12tracks13});
If you want to switch between the front and back facing cameras, starting from SDK version 2.7.0, you can restart the existing LocalVideoTrack.
1const { createLocalTracks, connect } = require('twilio-video');23const tracks = await createLocalTracks({4audio: true,5video: { facingMode: 'user' }6});78// Join the Room with the pre-acquired LocalTracks.9const room = await connect('token', {10name: 'my-cool-room',11tracks12});1314const cameraTrack = tracks.find(track => track.kind === 'video');1516// Switch to the back facing camera.17cameraTrack.restart({ facingMode: 'environment' });
In SDK versions 2.6.0 and below, you can stop and unpublish the existing LocalVideoTrack, use createLocalVideoTrack to create a new LocalVideoTrack and publish it to the Room.
1const { createLocalTracks, createLocalVideoTrack, connect } = require('twilio-video');23const tracks = await createLocalTracks({4audio: true,5video: { facingMode: 'user' }6});78// Join the Room with the pre-acquired LocalTracks.9const room = await connect('token', {10name: 'my-cool-room',11tracks12});1314// Capture the back facing camera.15const backFacingTrack = await createLocalVideoTrack({ facingMode: 'environment' });1617// Switch to the back facing camera.18const frontFacingTrack = tracks.find(track => track.kind === 'video');19frontFacingTrack.stop();20room.localParticipant.unpublishTrack(frontFacingTrack);21room.localParticipant.publishTrack(backFacingTrack);
In mobile browsers, getUserMedia is successful even when your microphone and/or camera are reserved by another tab or application. This can result in mobile Participants not being seen and/or heard by others in a Room. In order to work around this, we recommend that your application prompt users to test their microphone and camera before joining a Room. You can use createLocalAudioTrack to acquire the microphone, and use the Web Audio API to calculate its level. If the level is 0 even when the user is talking, then most likely the microphone is reserved by either another tab or application. You can then recommend that the user close all the other applications and reload your application, or worst case, restart the browser.
testmic.js
1const { createLocalAudioTrack } = require('twilio-video');2const pollAudioLevel = require('./pollaudiolevel');34const audioTrack = await createLocalAudioTrack();56// Display the audio level.7pollAudioLevel(audioTrack, level => {8/* Update audio level indicator. */9});
pollaudiolevel.js
1const AudioContext = window.AudioContext || window.webkitAudioContext;2const audioContext = AudioContext ? new AudioContext() : null;34function rootMeanSquare(samples) {5const sumSq = samples.reduce((sumSq, sample) => sumSq + sample * sample, 0);6return Math.sqrt(sumSq / samples.length);7}89async function pollAudioLevel(track, onLevelChanged) {10if (!audioContext) {11return;12}1314// Due to browsers' autoplay policy, the AudioContext is only active after15// the user has interacted with your app, after which the Promise returned16// here is resolved.17await audioContext.resume();1819// Create an analyser to access the raw audio samples from the microphone.20const analyser = audioContext.createAnalyser();21analyser.fftSize = 1024;22analyser.smoothingTimeConstant = 0.5;2324// Connect the LocalAudioTrack's media source to the analyser.25const stream = new MediaStream([track.mediaStreamTrack]);26const source = audioContext.createMediaStreamSource(stream);27source.connect(analyser);2829const samples = new Uint8Array(analyser.frequencyBinCount);30let level = null;3132// Periodically calculate the audio level from the captured samples,33// and if changed, call the callback with the new audio level.34requestAnimationFrame(function checkLevel() {35analyser.getByteFrequencyData(samples);36const rms = rootMeanSquare(samples);37const log2Rms = rms && Math.log2(rms);3839// Audio level ranges from 0 (silence) to 10 (loudest).40const newLevel = Math.ceil(10 * log2Rms / 8);41if (level !== newLevel) {42level = newLevel;43onLevelChanged(level);44}4546// Continue calculating the level only if the audio track is live.47if (track.mediaStreamTrack.readyState === 'live') {48requestAnimationFrame(checkLevel);49} else {50requestAnimationFrame(() => onLevelChanged(0));51}52});53}5455module.exports = pollAudioLevel;
You can use createLocalVideoTrack to acquire the camera, and attach its corresponding <video>
element to the DOM. If there are no video frames, then most likely the camera is reserved by either another tab or application. Your can then recommend that the user close all the other applications and reload your application, or worst case, restart the browser.
testcamera.js
1const { createLocalVideoTrack } = require('twilio-video');23const videoTrack = await createLocalVideoTrack();45// Display the video preview.6const divContainer = document.getElementById('local-video');7const videoElement = videoTrack.attach();8divContainer.appendChild(videoElement);
NOTE: In iOS Safari, because of this WebKit bug, calling getUserMedia again will mute previously acquired LocalTracks. So, please make sure that the LocalTracks that you pass in ConnectOptions are neither muted nor stopped.
When an application that is running on a mobile browser is backgrounded, it will not have access to the video feed from the camera until it is foregrounded. So, we recommend that you stop and unpublish the camera's LocalVideoTrack, and publish a new LocalVideoTrack once your application is foregrounded. On the remote side, you can listen to the unsubscribed and subscribed events on the corresponding RemoteVideoTrackPublication in order to notify the user accordingly. You can use the Page Visibility API to detect backgrounding and foregrounding.
mobileuser.js
1const { connect, createLocalTracks, createLocalVideoTrack } = require('twilio-video');23const tracks = await createLocalTracks();45let videoTrack = tracks.find(track => track.kind === 'video');67const room = await connect('token1', {8name: 'my-cool-room',9tracks10});1112if (/* isMobile */) {13document.addEventListener('visibilitychange', async () => {14if (document.visibilityState === 'hidden') {15// The app has been backgrounded. So, stop and unpublish your LocalVideoTrack.16videoTrack.stop();17room.localParticipant.unpublishTrack(videoTrack);18} else {19// The app has been foregrounded, So, create and publish a new LocalVideoTrack.20videoTrack = await createLocalVideoTrack();21await room.localParticipant.publishTrack(videoTrack);22}23});24}
remoteuser.js
1const { connect } = require('twilio-video');23function setupRemoteVideoNotifications(publication) {4if (publication.isSubscribed) {5// Indicate to the user that the mobile user has added video.6}78publication.on('subscribed', track => {9// Indicate to the user that the mobile user has added video.10});1112publication.on('unsubscribed', track => {13// Indicate to the user that the mobile user has removed video.14});15}1617function setupRemoteVideoNotificationsForParticipant(participant) {18// Set up remote video notifications for the VideoTracks that are19// already published.20participant.videoTracks.forEach(setupRemoteVideoNotifications);2122// Set up remote video notifications for the VideoTracks that will be23// published later.24participant.on('trackPublished', setupRemoteVideoNotifications);25}2627const room = await connect('token2', { name: 'my-cool-room' });2829// Set up remote video notifications for the VideoTracks of RemoteParticipants30// already in the Room.31room.participants.forEach(setupRemoteVideoNotificationsForParticipant);3233// Set up remote video notifications for the VideoTracks of RemoteParticipants34// that will join the Room later.35room.on('participantConnected', setupRemoteVideoNotificationsForParticipant);
When the user closes the tab/browser or navigates to another web page, we recommend that you disconnect from the Room so that other Participants are immediately notified.
1const { createLocalTracks, connect } = require('twilio-video');23const tracks = await createLocalTracks();45const room = await connect('token', {6name: 'my-cool-room',7tracks8});910// Listen to the "beforeunload" event on window to leave the Room11// when the tab/browser is being closed.12window.addEventListener('beforeunload', () => room.disconnect());1314// iOS Safari does not emit the "beforeunload" event on window.15// Use "pagehide" instead.16window.addEventListener('pagehide', () => room.disconnect());
This section lists some of the important errors raised by twilio-video.js
and provides recommendations on how best to handle them.
These errors are raised when twilio-video.js fails to acquire the user's local media (camera and/or microphone). Your app can catch these errors as shown below:
1const { connect, createLocalAudioTrack, createLocalTracks, createLocalVideoTrack } = require('twilio-video');23function handleMediaError(error) {4console.error('Failed to acquire media:', error.name, error.message);5}67// Handle media error raised by createLocalAudioTrack.8createLocalAudioTrack().catch(handleMediaError);910// Handle media error raised by createLocalVideoTrack.11createLocalVideoTrack().catch(handleMediaError);1213// Handle media error raised by createLocalTracks.14createLocalTracks().catch(handleMediaError);1516const mediaErrors = [17'NotAllowedError',18'NotFoundError',19'NotReadableError',20'OverconstrainedError',21'TypeError'22];2324// Since connect() will acquire media for the application if tracks are not provided in ConnectOptions,25// it can raise media errors.26connect(token, { name: 'my-cool-room' }).catch(error => {27if (mediaErrors.includes(error.name)) {28// Handle media error here.29handleMediaError(error);30}31});
The following table describes the possible media errors and proposes ways for the application to handle them:
Name | Message | Cause | Solution |
---|---|---|---|
NotFoundError | 1. Permission denied by system 2. The object cannot be found here 3. Requested device not found | 1. User has disabled the input device for the browser in the system settings 2. User's machine does not have any such input device connected to it | 1. User should enable the input device for the browser in the system settings 2. User should have at lease one input device connected |
NotAllowedError | 1. Permission denied 2. Permission dismissed 3. The request is not allowed by the user agent or the platform in the current context 4. The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission | 1. User has denied permission for your app to access the input device, either by clicking the "deny" button on the permission dialog, or by going to the browser settings 2. User has denied permission for your app by dismissing the permission dialog | 1. User should allow your app to access the input device in the browser settings and then reload 2. User should reload your app and grant permission to access the input device |
TypeError | 1. Cannot read property 'getUserMedia' of undefined 2. navigator.mediaDevices is undefined | Your app is being served from a non-localhost non-secure context | Your app should be served from a secure context (localhost or https) |
NotReadableError | 1. Failed starting capture of a audio track 2. Failed starting capture of a video track 3. Could not start audio source 4. Could not start video source 5. The I/O read operation failed | The browser could not start media capture with the input device even after the user gave permission, probably because another app or tab has reserved the input device | User should close all other apps and tabs that have reserved the input device and reload your app, or worst case, restart the browser |
OverconstrainedError | N/A | The input device could not satisfy the requested media constraints | If this exception was raised due to your app requesting a specific device ID, then most likely the input device is no longer connected to the machine, so your app should request the default input device |
NOTE: Each error can log a different message depending on the browser and OS. This table lists all possible messages associated with each error.
These errors are raised by twilio-video.js when it fails to join a Room. Your app can catch these errors as shown below:
1const { connect } = require('twilio-video');23connect(token, { name: 'my-cool-room' }).catch(error => {4if ('code' in error) {5// Handle connection error here.6console.error('Failed to join Room:', error.code, error.message);7}8});
The following table describes the most common connection errors and proposes ways for the application to handle them:
Error | Code | Cause | Solution |
---|---|---|---|
SignalingConnectionError | 53000 | The client could not establish a connection to Twilio's signaling server | User should make sure to have a stable internet connection |
SignalingServerBusyError | 53006 | Twilio's signaling server is too busy to accept new clients | User should try joining the Room again after some time |
RoomMaxParticipantsExceededError | 53105 | The Room cannot allow in any more Participants to join | Your app should notify the user that the Room is full |
RoomNotFoundError | 53106 | The client attempted to connect to a Room that does not exist | If ad-hoc Room creation is disabled, then your app should make sure that the Room is created using the REST API before clients attempt to join |
MediaConnectionError | 53405 | The client failed to establish a media connection with the Room | 1. User should make sure to have a stable internet connection 2. If the user is behind a firewall, then it should allow media traffic to and from Twilio to go through |
These errors are raised by twilio-video.js when it is inadvertently disconnected from the Room. Your app can catch these errors as shown below:
1const { connect } = require('twilio-video');23connect(token, { name: 'my-cool-room' }).then(room => {4room.once('disconnected', (room, error) => {5if (error) {6console.log('You were disconnected from the Room:', error.code, error.message);7}8});9});
The following table describes the most common disconnection errors and proposes ways for the application to handle them:
Error | Code | Cause | Solution |
---|---|---|---|
SignalingConnectionDisconnectedError | 53001 | The client failed to reconnect to Twilio's signaling server after a network disruption or handoff | User should make sure to have a stable internet connection |
SignalingConnectionTimeoutError | 53002 | The liveliness checks for the connection to Twilio's signaling server failed, or the current session expired | User should rejoin the Room |
ParticipantDuplicateIdentityError | 53205 | Another client joined the Room with the same identity | Your app should make sure each client creates an AccessToken with a unique identity string |
MediaConnectionError | 53405 | The client failed to re-establish its media connection with the Room after a network disruption or handoff | 1. User should make sure to have a stable internet connection 2. If the user is behind a firewall, then it should allow media traffic to and from Twilio to go through |
This section lists some of the important warnings raised by twilio-video.js
and provides recommendations on how best to handle them.
The Media Warnings feature is currently in Public Beta. Learn more about Twilio's beta product support here.
The JavaScript SDK raises Media Warnings whenever the Twilio media server is not able to detect media from a published audio or video track. You can enable Media Warnings starting from version 2.22.0 of the Twilio Video JavaScript SDK.
You can enable Media Warnings with the notifyWarnings
option in the SDK's ConnectOptions object when connecting to a Twilio Room:
1// Enable Media Warnings2const room = await connect('token', {3notifyWarnings: [ 'recording-media-lost' ]4// Other connect options5});
notifyWarnings
takes an array of warnings to listen for. By default, this array is empty and no warning events will be raised.
Possible values to provide in the notifyWarnings
array are:
recording-media-lost
- Raised when the media server has not detected any media on the published track that is being recorded in the past 30 seconds. This usually happens when there are network interruptions or when the track has stopped.The SDK raises Media Warning events when it detects the conditions specified in the notifyWarnings
options above. You can implement callbacks on these events to act on them when they happen, or to alert the user of an issue.
The warningsCleared
event is raised when conditions have returned to normal.
1// Catch Media Warnings2Array.from(room.localParticipant.tracks.values()).forEach(publication => {3publication.on('warning', name => {4if (name === 'recording-media-lost') {5console.log(`LocalTrack ${publication.track.name} is not recording media.`);67// Wait a reasonable amount of time to clear the warning.8const timer = setTimeout(() => {9// If the warning is not cleared, you can manually10// reconnect to the room, or show a dialog to the user11}, 5000);1213publication.once('warningsCleared', () => {14console.log(`LocalTrack ${publication.track.name} warnings have cleared!`);15clearTimeout(timer);16});17}18});19});