Skip to content

Experimental: run web rooms on livekit-client - #1337

Draft
hiroshihorie wants to merge 7 commits into
mainfrom
hiroshi/web-js-backend
Draft

hiroshihorie wants to merge 7 commits into
mainfrom
hiroshi/web-js-backend

Conversation

@hiroshihorie

Copy link
Copy Markdown
Member

Summary

Draft to explore running the web build on livekit-client (the JS SDK) instead of the Dart engine, so web keeps a maintained engine if native moves to the Rust core.

RoomBackend (lib/src/core/backend/) is an internal seam. Room, participants and publications stay the public API. When a backend is set, Room sends connect, disconnect, publish, mute, data and subscription changes through it, and applies the protobuf snapshots it delivers through the existing update paths. Native has no backend and keeps the Dart engine unchanged. A Rust core could implement the same interface later.

On web, JsRoomBackend loads the bundled livekit-client 2.26.2 ES module, which owns signaling, peer connections, reconnects and simulcast. Dart keeps capture, rendering and adaptive stream (adaptiveStream is off in JS and Dart forwards the track settings). The server sees sdk FLUTTER with livekit-client in other_sdks. --dart-define=LIVEKIT_WEB_DART_ENGINE=true keeps the Dart engine on web.

The commits are atomic and meant to be reviewed one by one.

Verified (dart2js and wasm, local livekit-server)

  • connect, bad token gives ConnectException, disconnect
  • join snapshot, participants joining and leaving mid-session
  • publish camera and mic, subscribe and render
  • local mute and unmute
  • data both ways
  • room and participant metadata, active speakers
  • signal reconnect (resuming then reconnected) and full reconnect
  • adaptive stream picks 640x360 for a 640px view and 320x180 for a 160px view
  • example app in Chrome
  • flutter test: 722 passed, 1 skipped. analyze and format clean on every commit

Gaps on the JS backend

  • Throws UnsupportedError: E2EE, RPC, sending data streams, track subscription permissions
  • Missing without an error: incoming data streams, RPC and transcriptions, connection quality, stream state and permission events, null sender/receiver/transceiver, rtcConfiguration, simulcast layers and degradation preference not passed through, preconnect, connection check
  • Known bug: local track sids go stale after a full reconnect
  • Not verified: remote mute, media after a stop-on-mute restart, the JS publish option names. The LIVEKIT_WEB_DART_ENGINE build compiles but was not run

Notes

  • livekit-client keeps participantInfo protected and roomInfo private. The snapshots read them anyway, a public accessor upstream would make this safe across versions.
  • The 1.5 MB asset is limited to web with platforms: [web]. Flutter 3.41+ honors the filter, 3.38 to 3.40 bundle it on every platform.

RoomBackend lets Room hand its session to an implementation other than the
Dart engine. It speaks the LiveKit protocol: the backend reports
ParticipantInfo, TrackInfo and Room snapshots through RoomBackendDelegate,
so the existing update paths can apply them unchanged.

createPlatformRoomBackend picks the backend per platform through a
conditional import. It returns null everywhere for now, so the Dart engine
stays in use.
When the platform provides a backend, Room connects, disconnects and reads
its connection state through it instead of the engine. The join snapshot is
applied the same way the engine's join response is (room info, local
participant, existing remotes, then RoomConnectedEvent), and events the
backend holds back until then are released afterwards.

_RoomBackendSink applies backend reports through the paths engine events
take: participant updates and disconnects, subscribed and unsubscribed
tracks, active speakers, room updates, data packets, and resuming,
reconnecting, reconnected and disconnected events.

prepareConnection, getSid and sendSimulateScenario go through the backend.
E2EE, RPC and the remaining simulated scenarios throw UnsupportedError via
requireDartEngine. Registering a text or byte stream handler logs a warning,
incoming streams are not delivered by a backend yet.

No behavior change on platforms without a backend.
With a backend set, local participants publish the media track through it
(capture stays in Dart, the track is started first) and finish the
publication through the shared _didPublishViaBackend. Unpublish, mute,
publishData, setMetadata, setName and setAttributes go to the backend, and
mute reconciliation on participant updates is left to it.

Remote publications send subscription changes and track settings to the
backend, so adaptive stream decisions made in Dart still reach the server.

LocalTrack.backendReplaceTrack hands a restarted media track to the backend,
which owns the sender, so stop-on-mute and camera switches keep working.

Data stream sends and track subscription permissions throw
UnsupportedError on a backend.
scripts/fetch_livekit_js.sh copies the ES module build of livekit-client
2.26.2 into assets/js/ (with its license, without the source map
reference). The pubspec entry is limited to web. Flutter 3.41+ leaves it out
of native builds, older versions ignore the filter and bundle it everywhere.
JsRoomBackend runs the session on livekit-client, loaded once per page from
the bundled asset with importModule. The client reports itself as FLUTTER
with livekit-client in other_sdks.

livekit-client owns signaling, peer connections, reconnects and simulcast.
Its adaptive stream is off and tracks are published as user provided, so
Dart keeps deciding what each view needs and keeps owning capture.

Participant, track and room snapshots are read as protobuf bytes from
livekit-client (participantInfo is a protected field there) and delivered
through one ordered queue. Snapshots are read when the queued task runs,
because livekit-client announces a participant before storing its info.
Listeners are guarded and declare the exact argument count of their event,
a throw would land in livekit-client's emit and break its signal handler.

--dart-define=LIVEKIT_WEB_DART_ENGINE=true keeps the Dart engine on web.
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

This PR bumps

  • livekit_client: minor

This branch has not been deployed

No deployments
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.

1 participant