Skip to content

Support for large RPC messages using compression + data streams#1832

Open
1egoman wants to merge 23 commits intomainfrom
rpc-with-compression-data-streams
Open

Support for large RPC messages using compression + data streams#1832
1egoman wants to merge 23 commits intomainfrom
rpc-with-compression-data-streams

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented Mar 11, 2026

Currently this prototype does the below:

  • Use the existing RPCRequest path when the advertised clientProtocol is less than 1.
  • When under 15kb, compress the payload and include this on the RPCRequest inline (compressedPayload)
  • Above 15kb, compress and send via data streams.

Also undertakes some signifigant refactoring to move RPC logic out into a RpcClientManager / RpcServerManager, which given the increase in complexity makes the code a little less fragmented / easier to reason about.

Todo

  • Add changeset
  • Tests have not been updated, so do that
  • Add tests for the new non "legacy" paths - compression and data streams
  • I currently have the benchmark I ran checked in under examples/rpc-benchmark - this is what is leading to the large diff size. Before potentially merging this, think about if it makes sense to keep this checked in or not. And if so, go through and update some of the docs to be a little more modern / correct (in particular, it still refers to the "legacy path < 1kb" stuff).

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 11, 2026

⚠️ No Changeset found

Latest commit: b6c177e

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 Mar 11, 2026

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 88.03 KB (+1.49% 🔺)
dist/livekit-client.umd.js 98.59 KB (+1.36% 🔺)

@1egoman 1egoman force-pushed the rpc-with-compression-data-streams branch from 6128790 to bc5f66d Compare March 13, 2026 17:15
@1egoman 1egoman marked this pull request as ready for review March 27, 2026 15:56
@1egoman 1egoman changed the title (WIP) Support for large RPC messages using compression + data streams Support for large RPC messages using compression + data streams Mar 27, 2026
Comment on lines +53 to +69
constructor(
engine: RTCEngine,
log: StructuredLogger,
outgoingDataStreamManager: OutgoingDataStreamManager,
getRemoteParticipantClientProtocol: (identity: Participant['identity']) => number,
) {
this.engine = engine;
this.log = log;
this.outgoingDataStreamManager = outgoingDataStreamManager;
this.getRemoteParticipantClientProtocol = getRemoteParticipantClientProtocol;
}

setupEngine(engine: RTCEngine) {
this.engine = engine;

this.engine.on(EngineEvent.DataPacketReceived, this.handleDataPacket);
}
Copy link
Copy Markdown
Contributor Author

@1egoman 1egoman Mar 27, 2026

Choose a reason for hiding this comment

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

Note to self: in attempting to update the tests for this, I realized that tightly coupling both RpcClientManager / RpcServerManager to RTCEngine is a bad idea. It should work more like the data tracks managers work where there are incoming and outgoing events, and the glue to wire those events up to the engine happens at the room level.

1egoman added 19 commits March 27, 2026 17:59
This is so that this client protocol value can be used to know what
version of RPC that a remote client supports.
…kas did in the original web example

Register a data stream with an attribute, and listen for data streams
with that attribute on the other end.
…r rpc calls that take a long time

If a RPC call takes a long time and a participant disconnects half way
through, just drop the return value.
After benchmarking this proved to be not very useful in practice,
compression is basically the same.
@1egoman 1egoman force-pushed the rpc-with-compression-data-streams branch from fa9fde9 to b6c177e Compare March 27, 2026 22:01
@1egoman
Copy link
Copy Markdown
Contributor Author

1egoman commented Mar 27, 2026

(rebased on top of latest main)

* Compress a string payload using gzip.
* @internal
*/
export async function gzipCompress(data: string): Promise<Uint8Array> {
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.

for both compress helpers we need to handle unsupported browsers: https://caniuse.com/mdn-api_compressionstream

for regular payloads this should probably fall back to 'regular' in these cases

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