Skip to content

Add local data track flush method#1925

Open
1egoman wants to merge 8 commits intomainfrom
add-local-data-track-flush
Open

Add local data track flush method#1925
1egoman wants to merge 8 commits intomainfrom
add-local-data-track-flush

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented Apr 28, 2026

When called, waits for all in flight packets to be sent before resolving. Use this to:

  1. Send frames exactly in order:
await localDataTrack.tryPush(/* ... */);
await localDataTrack.flush();

await localDataTrack.tryPush(/* ... */);
await localDataTrack.flush();

// ... etc ...
  1. Wait for frames to all be delivered before unpublishing a local data track:
await localDataTrack.tryPush(/* ... */);
await localDataTrack.tryPush(/* ... */);
await localDataTrack.tryPush(/* ... */);
// ... etc ...
await localDataTrack.flush();
await localDataTrack.unpublish();

When called, waits for all in flight packets to be sent before resolving. Use this to:

1. Send frames exactly in order:
```ts
await track.tryPush(/* ... *\/);
await track.flush();
await track.tryPush(/* ... *\/);
await track.flush();
// ... etc ...
```

2. Wait for frames to all be delivered before unpublishing a local data track:

```ts
await track.tryPush(/* ... *\/);
await track.tryPush(/* ... *\/);
await track.tryPush(/* ... *\/);
// ... etc ...
await track.flush();
await track.unpublish();
```
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

🦋 Changeset detected

Latest commit: 725d1f3

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 97.24 KB (+0.31% 🔺)
dist/livekit-client.umd.js 106.11 KB (+0.21% 🔺)

protected log: StructuredLogger = log;

/** Resolves once the data track has sent all pending packets the rtc data channel buffer. */
protected flushedFuture = new Future<void, never>();
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 should happen to this future if the room disconnects while flushing?

Copy link
Copy Markdown
Contributor Author

@1egoman 1egoman Apr 29, 2026

Choose a reason for hiding this comment

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

Good point - I think the two options are rejecting with an error or just resolving it normally. Also I would argue that the better signal might be engine close, not room disconnect, though I think in practice the former should always happen when the latter occurs.

Any thoughts on either of these points before I add this?

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.

engine close also sounds fine to me!

no strong opinion on whether it should be rejecting or resolving, but something should happen!

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.

Sweet, done in 8c3b58e and da85f11 (new test).

Comment thread src/room/Room.ts
1egoman added 6 commits April 29, 2026 11:23
…nnect and use this to resolve any pending flushes
…nts works

If this isn't set, then an event with an `undefined` payload is
indistinguishable from an event not being delivered because buffer.pop()
returns T | undefined.
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.

2 participants