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 to learn about the DataTrack API.
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.
The LocalDataTrack is a Track that represents data that can be published to a Room by the LocalParticipant.
1const { LocalDataTrack } = require(`twilio-video`);2const dataTrack = new LocalDataTrack();
Next, we want to publish the LocalDataTrack we created earlier to the Room we connect to:
1const { connect } = require('twilio-video');23const room = await connect('$TOKEN', {4name: 'my-chat-room',5tracks: [dataTrack]6});
You can use send 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:
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:
1const dataTrackPublished = {};23dataTrackPublished.promise = new Promise((resolve, reject) => {4dataTrackPublished.resolve = resolve;5dataTrackPublished.reject = reject;6});78room.localParticipant.on('trackPublished', publication => {9if (publication.track === dataTrack) {10dataTrackPublished.resolve();11}12});1314room.localParticipant.on('trackPublicationFailed', (error, track) => {15if (track === dataTrack) {16dataTrackPublished.reject(error);17}18});1920function sendMessage(message) {21dataTrackPublished.promise.then(() => dataTrack.send(message));22}23
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.
1participant.on('trackSubscribed', track => {2console.log(`Participant "${participant.identity}" added ${track.kind} Track ${track.sid}`);3if (track.kind === 'data') {4track.on('message', data => {5console.log(data);6});7}8});
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:
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.