Skip to content

Conversation

@1egoman
Copy link
Contributor

@1egoman 1egoman commented Aug 4, 2025

Problem / Concern

RTCEngine gets fully town down and recreated initially when an initial connection is created and also whenever a reconnection occurs. This means that any class that has a reference to engine must be informed of this new change so that it can keep its reference to engine current.

The current means this is being done is by calling a setupEngine method on all dependents - right now, LocalParticipant and E2EEManager, plus soon OutgoingDataStreamManager. This is relatively coupled though because the room needs to call that method on each of these classes!

Potential solution

The existing approach works, but is inelegant. To attempt to solve this in a slightly more elegant way, I've added a new SupersededBy event to the RTCEngine which is fired on the old engine whenever a new engine is generated, which allows anybody who has the engine to always have the latest copy.

@changeset-bot
Copy link

changeset-bot bot commented Aug 4, 2025

⚠️ No Changeset found

Latest commit: aab40b1

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
Contributor

github-actions bot commented Aug 4, 2025

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 79.83 KB (+0.04% 🔺)
dist/livekit-client.umd.js 85.81 KB (+0.18% 🔺)

@1egoman 1egoman requested a review from lukasIO August 4, 2025 16:16
@1egoman 1egoman marked this pull request as ready for review August 4, 2025 16:16
Comment on lines 613 to 615

if (this.localParticipant) {
this.localParticipant.setupEngine(this.engine);
}
if (this.e2eeManager) {
this.e2eeManager.setupEngine(this.engine);
}
oldEngine?.emit(EngineEvent.SupersededBy, this.engine);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, n function calls were required. Now, only one publish does the same thing!

Comment on lines 96 to +100

const EngineEventEmitter = EventEmitter as new () => TypedEventEmitter<EngineEventCallbacks>;

/** @internal */
export default class RTCEngine extends (EventEmitter as new () => TypedEventEmitter<EngineEventCallbacks>) {
export default class RTCEngine extends EngineEventEmitter {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Admittedly this is a little weird - EngineEventCallbacks now contains a reference to RTCEngine, so putting this expression directly within the extends creates a reference cycle that typescript doesn't seem to like. Breaking it up like this seems to solve that problem, though.

Copy link
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.

nice!

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