Module: twilio-video

Properties:
Name Type Description
isSupported boolean

true if the current browser is officially supported by twilio-video.js; In this context, "supported" means that twilio-video.js has been extensively tested with this browser; This table specifies the list of officially supported browsers.

Logger object

The loglevel module used by the SDK. Use this object to access the internal loggers and perform actions as defined by the loglevel APIs. See connect for examples.

version string

current version of twilio-video.js.

Methods


<static> connect(token [, options])

Connect to a Room.

By default, this will automatically acquire an array containing a LocalAudioTrack and LocalVideoTrack before connecting to the Room. These will be stopped when you disconnect from the Room.

You can override the default behavior by specifying options. For example, rather than acquiring a LocalAudioTrack and LocalVideoTrack automatically, you can pass your own array which you can stop yourself. See ConnectOptions for more information.

Parameters:
Name Type Argument Description
token string

The Access Token string

options ConnectOptions <optional>

Options to override the default behavior, invalid options are ignored.

Throws:
Returns:
Type
CancelablePromise.<Room>
Examples
var Video = require('twilio-video');
var token = getAccessToken();
Video.connect(token, {
  name: 'my-cool-room'
}).then(function(room) {
  room.on('participantConnected', function(participant) {
    console.log(participant.identity + ' has connected');
  });
  room.once('disconnected', function() {
    console.log('You left the Room:', room.name);
  });
}).catch(error => {
  console.log('Could not connect to the Room:', error.message);
});
var Video = require('twilio-video');
var token = getAccessToken();

// Connect with audio-only
Video.connect(token, {
  name: 'my-cool-room',
  audio: true
}).then(function(room) {
  room.on('participantConnected', function(participant) {
    console.log(participant.identity + ' has connected');
  });

  room.once('disconnected', function() {
    console.log('You left the Room:', room.name);
  });
}).catch(error => {
  console.log('Could not connect to the Room:', error.message);
});
var Video = require('twilio-video');
var token = getAccessToken();

// Connect with media acquired using getUserMedia()
navigator.mediaDevices.getUserMedia({
  audio: true,
  video: true
}).then(function(mediaStream) {
  return Video.connect(token, {
    name: 'my-cool-room',
    tracks: mediaStream.getTracks()
  });
}).then(function(room) {
  room.on('participantConnected', function(participant) {
    console.log(participant.identity + ' has connected');
  });

  room.once('disconnected', function() {
    console.log('You left the Room:', room.name);
  });
}).catch(error => {
  console.log('Could not connect to the Room:', error.message);
});
var Video = require('twilio-video');
var token = getAccessToken();

// Connect with custom names for LocalAudioTrack and LocalVideoTrack
Video.connect(token, {
  name: 'my-cool-room'
  audio: { name: 'microphone' },
  video: { name: 'camera' }
}).then(function(room) {
  room.localParticipants.trackPublications.forEach(function(publication) {
    console.log('The LocalTrack "' + publication.trackName + '" was successfully published');
  });
}).catch(error => {
  console.log('Could not connect to the Room:', error.message);
});
// Accessing the SDK logger
var { Logger, connect } = require('twilio-video');
var token = getAccessToken();

var logger = Logger.getLogger('twilio-video');

// Listen for logs
var originalFactory = logger.methodFactory;
logger.methodFactory = function (methodName, logLevel, loggerName) {
  var method = originalFactory(methodName, logLevel, loggerName);

  return function (datetime, logLevel, component, message, data) {
    method(datetime, logLevel, component, message, data);
    // Send to your own server
    postDataToServer(arguments);
  };
};
logger.setLevel('debug');

connect(token, {
  name: 'my-cool-room'
}).then(function(room) {
  room.on('participantConnected', function(participant) {
    console.log(participant.identity + ' has connected');
  });
}).catch(error => {
  console.log('Could not connect to the Room:', error.message);
});

<static> createLocalAudioTrack( [options])

Request a LocalAudioTrack.

Parameters:
Name Type Argument Description
options CreateLocalTracksOptions | CreateLocalAudioTrackOptions <optional>

Options for requesting a LocalAudioTrack

Returns:
Type
Promise.<LocalAudioTrack>
Examples
var Video = require('twilio-video');

// Connect to the Room with just video
Video.connect('my-token', {
  name: 'my-cool-room',
  video: true
}).then(function(room) {
  // Add audio after connecting to the Room
  Video.createLocalAudioTrack().then(function(localTrack) {
    room.localParticipant.publishTrack(localTrack);
  });
});
var Video = require('twilio-video');

// Request the LocalAudioTrack with a custom name
// and krisp noise cancellation
Video.createLocalAudioTrack({
  name: 'microphone',
  noiseCancellationOptions: {
     vendor: 'krisp',
     sdkAssetsPath: '/twilio-krisp-audio-plugin/1.0.0/dist'
  }
});

<async, static> createLocalTracks( [options])

Request LocalTracks. By default, it requests a LocalAudioTrack and a LocalVideoTrack. Note that on 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.

Parameters:
Name Type Argument Description
options CreateLocalTracksOptions <optional>
Returns:
Type
Promise.<Array.<LocalTrack>>
Examples
var Video = require('twilio-video');
// Request audio and video tracks
Video.createLocalTracks().then(function(localTracks) {
  var localMediaContainer = document.getElementById('local-media-container-id');
  localTracks.forEach(function(track) {
    localMediaContainer.appendChild(track.attach());
  });
});
var Video = require('twilio-video');
// Request just the default audio track
Video.createLocalTracks({ audio: true }).then(function(localTracks) {
  return Video.connect('my-token', {
    name: 'my-cool-room',
    tracks: localTracks
  });
});
var Video = require('twilio-video');
// Request the audio and video tracks with custom names
Video.createLocalTracks({
  audio: { name: 'microphone' },
  video: { name: 'camera' }
}).then(function(localTracks) {
  localTracks.forEach(function(localTrack) {
    console.log(localTrack.name);
  });
});
var Video = require('twilio-video');
var localTracks;

// Pre-acquire tracks to display camera preview.
Video.createLocalTracks().then(function(tracks) {
 localTracks = tracks;
 var localVideoTrack = localTracks.find(track => track.kind === 'video');
 divContainer.appendChild(localVideoTrack.attach());
})

// Later, join the Room with the pre-acquired LocalTracks.
Video.connect('token', {
  name: 'my-cool-room',
  tracks: localTracks
});

<static> createLocalVideoTrack( [options])

Request a LocalVideoTrack. Note that on 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.

Parameters:
Name Type Argument Description
options CreateLocalTrackOptions <optional>

Options for requesting a LocalVideoTrack

Returns:
Type
Promise.<LocalVideoTrack>
Examples
var Video = require('twilio-video');

// Connect to the Room with just audio
Video.connect('my-token', {
  name: 'my-cool-room',
  audio: true
}).then(function(room) {
  // Add video after connecting to the Room
  Video.createLocalVideoTrack().then(function(localTrack) {
    room.localParticipant.publishTrack(localTrack);
  });
});
var Video = require('twilio-video');

// Request the default LocalVideoTrack with a custom name
Video.createLocalVideoTrack({ name: 'camera' }).then(function(localTrack) {
  console.log(localTrack.name); // 'camera'
});

<static> runPreflight(token, options)

Run a preflight test. This method will start a test to check the quality of network connection.

Parameters:
Name Type Description
token string

The Access Token string

options PreflightOptions

Options for the test

Returns:

preflightTest - An instance to be used to monitor progress of the test.

Type
PreflightTest
Example
var { runPreflight } = require('twilio-video');
var preflight = runPreflight(token, preflightOptions);
preflightTest.on('progress', progress => {
  console.log('preflight progress:', progress);
});

preflightTest.on('failed', (error, report) => {
  console.error('preflight error:', error, report);
});

preflightTest.on('completed', report => {
  console.log('preflight completed:', report));
});