Skip to content

Data track integration#1820

Open
1egoman wants to merge 98 commits intomainfrom
data-track-integration
Open

Data track integration#1820
1egoman wants to merge 98 commits intomainfrom
data-track-integration

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented Feb 20, 2026

Integrates the data track managers (both incoming and outgoing) into the existing sdk Room and RemoteParticipant interfaces. Once this is merged, data tracks is fully implemented!

Interface - Publishing

const localDataTrack = await room.publishDataTrack({name: "data track name"});
await localDataTrack.tryPush(new Uint8Array(/* ... */));

// Once you are done with the local data track, it can be unpublished:
await localDataTrack.unpublish();

Interface - Subscribing

// There are two ways to get a remote data track, either a room event:
room.on(RoomEvent.RemoteDataTrackPublished, async (remoteDataTrack) => {
  // ...
});

// Or a new field on `RemoteParticipant`:
const remoteDataTrack = remoteParticipant.dataTracks.get("track name"); // Get a data track which has been published
const remoteDataTrack = await remoteParticipant.dataTracks.getDeferred("track name"); // Get a data track which _will_ be published shortly

// Either way, once you have a remoteDataTrack, you can subscribe:
const stream = await remoteDataTrack.subscribe(/* optional abort signal */);
for await (const frame of stream) {
  console.log(`Received bytes from ${remoteDataTrack.info.name}:`, frame.payload);
}

Todo:

Resolves CLT-2582

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 20, 2026

⚠️ No Changeset found

Latest commit: f63d21c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 24, 2026

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 94.89 KB (+9.4% 🔺)
dist/livekit-client.umd.js 103.7 KB (+6.61% 🔺)

@1egoman 1egoman force-pushed the data-track-integration branch 2 times, most recently from 2f31fbc to 6a5960d Compare February 25, 2026 21:48
@1egoman 1egoman force-pushed the data-track-integration branch from 6a5960d to 2e6f8cf Compare March 5, 2026 20:51
@1egoman 1egoman changed the title (WIP) Data track integration Data track integration Mar 5, 2026
@ladvoc ladvoc self-requested a review March 6, 2026 22:09

/** Publish the track to the SFU. This must be done before calling {@link tryPush} for the first time. */
async publish(signal?: AbortSignal) {
try {
Copy link
Copy Markdown
Contributor

@lukasIO lukasIO Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should error early (or warn and return) if isPublished is true?

Copy link
Copy Markdown
Contributor Author

@1egoman 1egoman Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already an error that gets thrown within publishRequest if the track is already published, but I could probably make the error message a little nicer (right now it is one of those "panic" bare errors).

IMO in this case, I think an explicit throw makes sense because you really should not be publishing / unpublishing data tracks without some care. But looking at the existing tracks it looks like it just warns and that's it. So maybe it makes sense to convert this throw into a warning, not sure.

EDIT: more thoughts on this here!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From sync: giving some more thought to this, I would say publish should throw whereas unpublish should not. For publish, this is something the application might want to handle and respond to.

* */
async unpublish() {
if (!this.handle) {
throw DataTrackPushFrameError.trackUnpublished();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: might be enough to log a warning here and return early?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I am not sure - see my previous comment about the throw vs warning here. I like the hard error in that it is something that can be caught and explicitly handled though since this is checking an invariant. The existing track publication logic for audio / video tracks logs a warning so maybe it makes sense to update it to be consistent.

I guess one downside to the hard error is it opens this code up to a TOC-TOU issue where something like the below could happen:

if (track.isPublished) {
  // Imagine here the track is unpublished at the SFU level for some unrelated reason ...
  await track.unpublish(); // ... so this would then throw an error
}

I am definitely curious what others think here!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You make a good point; a data track unpublish can be SFU initiated (e.g., if publish data permission is revoked). Given that, I think it makes sense to treat this as a first-class error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, from @1egoman 's description I'd be more inclined to treat it similarly to how other unpublish requests are handled, which is warning only

@1egoman 1egoman force-pushed the data-track-integration branch from 2619816 to 8e7a138 Compare March 16, 2026 17:40
1egoman added a commit that referenced this pull request Mar 16, 2026
@1egoman 1egoman marked this pull request as ready for review March 16, 2026 18:38
1egoman added 2 commits March 18, 2026 15:35
This allows multiple different versions of the app to run and all join
the same room
1egoman added 9 commits March 23, 2026 16:44
…dle the separate data tracks data channel

The old way this worked, channelKind was being computed based on
maxRetransmits === 0. But now there are multiple lossy data channels,
the regular one and the data tracks one, so this heuristic no longer
works.
…s are buffered when the dc is in low status

This was encountered when implementing the e2e tests - a large data
track frame could be split up into potentially n packets, and if one
packet is dropped the whole frame is considered dropped. So the behavior
that is really wanted here is to buffer packets and send them once the
data channel can accept more data, even though it is technically a lossy
data channel.
…channels

When operating in dual peer connection contexts, this is required.
@1egoman 1egoman requested a review from lukasIO March 26, 2026 19:37
Copy link
Copy Markdown
Contributor

@lukasIO lukasIO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a couple of nits that would be nice to address.

More generally and important: what happens currently when running this against older servers that don't have DataTrack support?

return;
if (!this.isBufferStatusLow(kind)) {
// Depending on the exact circumstance that data is being sent, either drop or wait for the
// buffer status to not be low before continuing.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the intention behind this differentiation? Why not just drop them for lossy?

Copy link
Copy Markdown
Contributor Author

@1egoman 1egoman Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I originally was doing but this didn't work for large data track payloads - here is some more context as to why from some notes I wrote up:

  • If the _data_tracks rtc data channel buffer was full, the existing logic would drop packets (what the lossy data channel currently does).
    • This doesn't work though for data track packets, because if the first n packets of a frame get enqueued but the last packet(s) get dropped because there isn't enough room in the rtc data channel buffer, the receiver only receives a partial set of packets which will never fully decode into a data track frame.
    • If packets aren't buffered like this, then it becomes impossible to send data track payloads > ~75k bytes (the e2e tests are checking this case)
    • Solution (commit): buffer data track packets instead of dropping them if the rtc data channel buffer is full

If there's some other way to handle this, I'm open to alternatives but what I did here was the most straightforward way I could think of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants