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 iOS DataTrack Example to learn about the DataTrack API.
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.
unidirectional
.
support reliable transmission
. Check out the section on Configuring DataTrack reliability.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 iOS SDK.
The TVILocalDataTrack is a Track that represents data that can be published to a Room by the TVILocalParticipant.
var localDataTrack = LocalDataTrack()
Next, we want to connect to a Room with the TVILocalDataTrack
we created earlier.
1let connectOptions = ConnectOptions(token: accessToken){ (builder) in2builder.roomName = "my-room"3if let localDataTrack = self.localDataTrack {4builder.dataTracks = [localDataTrack]5}6}7var room = TwilioVideoSDK.connect(options: connectOptions, delegate: self)
In the previous example, TVILocalDataTrack
was published when connecting to the Room. You can also publish TVILocalDataTrack
after connecting to a Room:
1if let localParticipant = room.localParticipant,2let localDataTrack = self.localDataTrack {3localParticipant.publishDataTrack(localDataTrack)4}
The DataTrack API supports sending string as well as byte data. You can use sendString or sendData 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.
1// MARK: LocalParticipantDelegate2extension MyClass : LocalParticipantDelegate {3func localParticipantDidPublishDataTrack(participant: LocalParticipant, dataTrackPublication: LocalDataTrackPublication) {4// The data track has been published and is ready for use56guard let localDataTrack = dataTrackPublication.localTrack else {7return8}910let message = "Hello DataTrack!"11localDataTrack.send(message)1213// Assume bytes and length are defined elsewhere14var messageBuffer = Data(bytes: bytes, length: length)15localDataTrack.send(messageBuffer)16}17}
The TVIRemoteParticipant class provides a delegate protocol named TVIRemoteParticipantDelegate. You can implement this protocol to learn about published and unpublished DataTrack events.
1// MARK: RemoteParticipantDelegate2extension MyClass : RemoteParticipantDelegate {34// Participant has published a data track.5func remoteParticipantDidPublishDataTrack(participant: RemoteParticipant,6publication: RemoteDataTrackPublication) {7}89// Participant has unpublished a data track.10func remoteParticipantDidUnpublishDataTrack(participant: RemoteParticipant,11publication: RemoteDataTrackPublication) {12}1314// Data track has been subscribed to and messages can be observed.15func didSubscribeToDataTrack(dataTrack: RemoteDataTrack,16publication: RemoteDataTrackPublication,17participant: RemoteParticipant) {18// Respond to incoming messages.19dataTrack.delegate = self20}2122// Data track has been unsubsubscribed from and messages cannot be observed.23func didUnsubscribeFromDataTrack(dataTrack: RemoteDataTrack,24publication: RemoteDataTrackPublication,25participant: RemoteParticipant) {26}27}
You can implement TVIRemoteDataTrackDelegate to receive incoming messages on a DataTrack.
1// MARK: RemoteDataTrackDelegate2extension ViewController : RemoteDataTrackDelegate {3func remoteDataTrackDidReceiveString(remoteDataTrack: RemoteDataTrack, message: String) {4}56func remoteDataTrackDidReceiveData(remoteDataTrack: RemoteDataTrack, message: Data) {7}8}
For a fully working example and to learn more about Data Tracks, take a look at the iOS DataTrack Example.
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 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.