Skip to contentSkip to navigationSkip to topbar
On this page

Using the DataTrack API with Android v5.x


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 Android DataTrack Example(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 channel which can be used to send low latency messages to zero or more receivers subscribed to the data. DataTracks have the following properties.

  • DataTracks are unidirectional.
  • DataTracks have built-in mechanisms to support reliable transmission. Check out the section on Configuring DataTrack reliability.
  • Recommended maximum payload size of data sent over the DataTrack is 16KiB.
  • string or byte data can be sent over the DataTrack.

In the next section we will show you how to use the DataTrack API with the Android 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)

LocalDataTrack localDataTrack = LocalDataTrack.create(context);

Connect to a Room with a LocalDataTrack

connect-to-a-room-with-a-localdatatrack page anchor

Next, we want to connect to a Room with the LocalDataTrack we created earlier

1
ConnectOptions connectOptions = new ConnectOptions.Builder(token)
2
.dataTracks(Collections.singletonList(localDataTrack))
3
.build();
4
Video.connect(context, connectOptions, roomListener);

Publish the LocalDataTrack

publish-the-localdatatrack page anchor

After connecting to the Room, we now want to publish our LocalDataTrack to it.

1
LocalParticipant localParticipant = room.getLocalParticipant();
2
localParticipant.publish(localDataTrack);

Send messages over the LocalDataTrack

send-messages-over-the-localdatatrack page anchor

The DataTrack API supports sending string as well as byte data. You can use one of the two variants of send(link takes you to an external page) to send a string or byte 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.

1
public class MyClass implements LocalParticipant.Listener {
2
3
// NOTE: Other LocalParticipant.Listener methods not implemented for brevity
4
5
@Override
6
public void onDataTrackPublished(@NonNull LocalParticipant localParticipant, @NonNull LocalDataTrackPublication localDataTrackPublication) {
7
8
// The data track has been published and is ready for use
9
string message = "hello DataTrack!"
10
localDataTrackPublication.getLocalDataTrack().send(message);
11
12
ByteBuffer messageBuffer = ByteBuffer.wrap(new byte[]{ 0xf, 0xe });
13
localDataTrackPublication.getLocalDataTrack().send(messageBuffer);
14
}
15
}

Listening for DataTrack events

listening-for-datatrack-events page anchor

The RemoteParticipant(link takes you to an external page) class provides a listener interface. You can implement this interface to listen to published and unpublished DataTrack events.

1
RemoteParticipant.Listener participantListener = new RemoteParticipant.Listener() {
2
3
// Participant has published data track
4
@Override public void onDataTrackPublished(RemoteParticipant
5
remoteParticipant, RemoteDataTrackPublication
6
remoteDataTrackPublication) {}
7
8
// Participant has unpublished data track
9
@Override public void onDataTrackUnpublished(RemoteParticipant
10
remoteParticipant, RemoteDataTrackPublication
11
remoteDataTrackPublication) {}
12
13
// Data track has been subscribed to and messages can be observed.
14
@Override public void onDataTrackSubscribed(RemoteParticipant
15
remoteParticipant, RemoteDataTrackPublication
16
remoteDataTrackPublication,RemoteDataTrack remoteDataTrack) {}
17
18
// Data track has been unsubsubscribed from and messages cannot be
19
// observed.
20
@Override public void onDataTrackUnsubscribed(RemoteParticipant
21
remoteParticipant, RemoteDataTrackPublication
22
remoteDataTrackPublication, RemoteDataTrack remoteDataTrack) {}
23
};

Receiving messages from the DataTrack

receiving-messages-from-the-datatrack page anchor
1
RemoteDataTrack.Listener dataTrackListener = new RemoteDataTrack.Listener() {
2
3
@Override
4
public void onMessage(String message) {
5
// Should print "Hello DataTrack!"
6
Log.d(TAG, String.format("Received data track message: %s",
7
message));
8
}
9
}
10
11
@Override
12
public void onMessage(ByteBuffer message) {
13
Log.d(TAG, "Received message buffer on data track!");
14
}
15
};
16
remoteDataTrack.setListener(dataTrackListener);

Take a look at the Android Quickstart Application(link takes you to an external page) to learn more.


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.

DataTrack connections are established between Participants via the media server. Under the hood, there is one connection between a local Participant to the Media server and a second connection from the Media server to the remote Participant. 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.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.