Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Using the DataTrack API - JavaScript


(warning)

Warning

This page is for reference only. We are no longer onboarding new customers to Programmable Video. Existing customers can continue to use the product until December 5, 2024(link takes you to an external page).
We recommend migrating your application to the API provided by our preferred video partner, Zoom. We've prepared this migration guide(link takes you to an external page) to assist you in minimizing any service disruption.

In this guide, we will show you how to use the DataTrack API to send messages between Participants connected to a Room. With the DataTrack API you will be able to build powerful collaboration features such as whiteboarding, screen annotations, shared augmented reality apps and more. Use this guide along with our example app Quick Draw With Twilio(link takes you to an external page) to learn about the DataTrack API.


Overview

overview page anchor

The DataTrack API lets you create a DataTrack which can be used to send low latency messages to zero or more subscribers to the track. DataTracks have the following properties:

In the next section, we will show you how to use the DataTrack API with the JavaScript SDK.


Create a LocalDataTrack

create-a-localdatatrack page anchor

The LocalDataTrack(link takes you to an external page) is a Track that represents data that can be published to a Room by the LocalParticipant(link takes you to an external page).


_10
const { LocalDataTrack } = require(`twilio-video`);
_10
const dataTrack = new LocalDataTrack();

Connect to a Room and Publish the LocalDataTrack

connect-to-a-room-and-publish-the-localdatatrack page anchor

Next, we want to publish the LocalDataTrack we created earlier to the Room we connect to:


_10
const { connect } = require('twilio-video');
_10
_10
const room = await connect('$TOKEN', {
_10
name: 'my-chat-room',
_10
tracks: [dataTrack]
_10
});

Send Messages Over a LocalDataTrack

send-messages-over-a-localdatatrack page anchor

You can use send(link takes you to an external page) to send data to the Room. DataTracks behave similarly to audio and video Tracks in the sense that, Participants will only receive data that was sent after:

  • The LocalDataTrack was successfully published to the Room, and
  • The Participant subscribed to the DataTrack.

For example, if Alice starts sending a stream of consecutive natural numbers (one number per second), and Bob joins the Room and subscribes to Alice's DataTrack after 5 seconds while Charlie joins the Room and subscribes to Alice's DataTrack after 10 seconds, then Bob will receive all the numbers starting from 6, and Charlie will receive all the numbers starting from 11.

Continuing with the example from above:


_22
const dataTrackPublished = {};
_22
_22
dataTrackPublished.promise = new Promise((resolve, reject) => {
_22
dataTrackPublished.resolve = resolve;
_22
dataTrackPublished.reject = reject;
_22
});
_22
_22
room.localParticipant.on('trackPublished', publication => {
_22
if (publication.track === dataTrack) {
_22
dataTrackPublished.resolve();
_22
}
_22
});
_22
_22
room.localParticipant.on('trackPublicationFailed', (error, track) => {
_22
if (track === dataTrack) {
_22
dataTrackPublished.reject(error);
_22
}
_22
});
_22
_22
function sendMessage(message) {
_22
dataTrackPublished.promise.then(() => dataTrack.send(message));
_22
}

Now that we are sending messages over the LocalDataTrack, we want our Participants to subscribe to the published DataTrack and receive those messages.

In the "trackSubscribed" event listener, you want to look for the subscribed DataTrack by checking the kind property. Once you have the DataTrack, you can extract the message payload.


_10
participant.on('trackSubscribed', track => {
_10
console.log(`Participant "${participant.identity}" added ${track.kind} Track ${track.sid}`);
_10
if (track.kind === 'data') {
_10
track.on('message', data => {
_10
console.log(data);
_10
});
_10
}
_10
});


Configuring DataTrack Reliability

configuring-datatrack-reliability page anchor

DataTracks are intended for low-latency communication between Participants. Importantly, to optimize for lowest latency possible, delivery of DataTrack messages is not guaranteed. You can think of them more like UDP messages, rather than TCP.

You can configure the retry parameters for your DataTrack with the following options:

  • maxPacketLifeTime sets the time in milliseconds during which the DataTrack will transmit or retransmit a message until that message is acknowledged.
  • maxRetransmits sets the maximum number of retransmit attempts that will be made.

In Group Rooms, DataTrack connections are established between Participants via the media server. Under the hood, there is one connection between a LocalParticipant to the media server and a second connection from the media server to the RemoteParticipant. Twilio's media server configures the same maxPacketLifeTime value on each remote Participant's connection. Therefore, you should set the maxPacketLifetime to half the acceptable max lifetime for each message you send.


Rate this Page: