Twilio JavaScript SDK to VidyoClient JavaScript SDK
This guide walks you through how to migrate your existing Twilio Video implementation to Vidyo Javascript SDK. To complement this guide, we have also developed a sample app that can switch between the Twilio SDK and Vidyo SDK by simply using a build-time configuration. This app provides a handy “translation guide” between Twilio and VidyoClient (Connector) JavaScript SDK. Navigate here for complete source code reference:
If you are starting a new project from scratch, we recommend looking at:
Install
To begin, install the VidyoClient JavaScript SDK:
npm install vidyoclient-nativewebrtc-sdk
You can uninstall the Twilio SDK from your project by uninstalling the package:
npm uninstall twilio-video
Authorize
Twilio uses JSON Web Tokens (JWT) to generate a token for users to join sessions. Vidyo follows a different model: a meeting room is created on backend middleware (referred to as the VidyoPortal) which generates a meeting link, and users join the room through their browser by clicking on the meeting link. For more information visit Getting Started.
Join meeting room
Twilio requires a token to join a meeting room.
import * as TwilioVideo from 'twilio-video'
var twilioVideo = TwilioVideo
var twilioRoom
twilioVideo.connect(TOKEN, {name: 'yourName', audio: false, video: false, dominantSpeaker: true}). then((room) => {
twilioRoom = room
})
Video
While Twilio requires additional configuration to set the quality of the video, Vidyo handles the video quality automatically based on network conditions and device capabilities. In low bandwidth conditions Vidyo prioritizes audio over video, and shared content video tiles over participant video. Vidyo also supports configuration options to limit maximum resolution of local or remote video.
Render local video
Twilio has a concept of video and audio tracks. To render the self-view, Twilio appends a video element inside the specified div.
const wrapVideo = (video, id, dataId) => {
const div = document.createElement('div');
div.append(video);
if(id) {
div.id = id;
}
if(dataId) {
div.setAttribute('data-id', dataId);
}
return div;
}
const videoTrack = await twilioVideo.createLocalVideoTrack();
const trackElement = wrapVideo(videoTrack.attach());
window.currentLocalCamera = {
track: videoTrack,
trackElement
};
document.getElementById('renderer').appendChild(trackElement);
Turn the camera off
Since Twilio video is track based - you must loop through each video track to unpublish the video, stop the video camera, and remove the video element from the DOM:
twilioRoom?.localParticipant.videoTracks.forEach(track => {
track.unpublish();
track.track.disable();
});
Render remote user video
Twilio uses participantConnected and trackSubscribed event listeners to determine which remote videos to render:
twilioRoom.on('participantConnected', (participant) => {
participant.on('trackSubscribed', (track) => {
// a user turned on their video, render it
document.getElementById('twilio-user-view-div').appendChild(track.attach());
});
participant.on('trackUnsubscribed', (track) => {
// a user turned off their video, stop rendering it
var selfTwilioVideo = document.getElementById('twilio-user-view-div')
selfTwilioVideo.querySelector('video').remove()
})
})
Audio
Select local microphone
Since Twilio audio is track based - you must loop through each audio track to start the audio, and add the audio element to the DOM:
twilioVideo.createLocalAudioTrack().then((localAudioTrack) => {
twilioRoom.localParticipant.publishTrack(localAudioTrack);
const audioElement = localAudioTrack.attach();
document.body.appendChild(audioElement);
})
Mute/unmute microphone
Since Twilio API is track based - you must loop through each audio track to mute/unmute the microphone.
Mute:
twilioRoom.localParticipant.audioTracks.forEach((publication) => {
publication.track.disable()
})
Unmute:
twilioRoom.localParticipant.audioTracks.forEach((publication) => {
publication.track.enable()
})
Event listeners
Twilio has event listeners for participant management, allowing you to detect changes that occur during a meeting.
User joins or leaves a session:
twilioRoom.on('participantConnected', (participant) => {
// user joined
})
twilioVideo.on('participantDisconnected', (participant) => {
// user left
})
Active speaker changes:
twilioVideo.on('dominantSpeakerChanged', (participant) => {
// new active speaker
})
End session
twilioVideo.disconnect()
Additional information
This guide covers only the fundamentals for migrating an application from the Twilio JavaScript SDK to the Vidyo JavaScript SDK. If you do not see a piece of functionality described in this guide, please take a look at our API reference or the VidyoPlatform Github page. If you have any technical questions or run into issues with your migration, you can reach out to the Support team at support@vidyocloud.com
Last updated