Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-donkeys-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/rtc-node': patch
---

Bugfix: Queue FFI events from rust and always process them in order
23 changes: 20 additions & 3 deletions packages/livekit-rtc/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
private byteStreamHandlers = new Map<string, ByteStreamHandler>();
private textStreamHandlers = new Map<string, TextStreamHandler>();

private preConnectEvents: FfiEvent[] = [];

e2eeManager?: E2EEManager;
connectionState: ConnectionState = ConnectionState.CONN_DISCONNECTED;

Expand Down Expand Up @@ -180,6 +182,8 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
options,
});

FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);

const res = FfiClient.instance.request<ConnectResponse>({
message: {
case: 'connect',
Expand Down Expand Up @@ -210,8 +214,6 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
rp.trackPublications.set(publication.sid!, publication);
}
}

FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);
break;
case 'error':
default:
Expand Down Expand Up @@ -279,7 +281,22 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>

private onFfiEvent = (ffiEvent: FfiEvent) => {
if (!this.localParticipant || !this.ffiHandle || !this.info) {
throw TypeError('cannot handle ffi events before connectCallback');
this.preConnectEvents.push(ffiEvent);
return;
}

// process preConnectEvents if we received the connectCallback after the events were queued
for (const ev of this.preConnectEvents) {
this.processFfiEvent(ev);
}
this.preConnectEvents = [];

this.processFfiEvent(ffiEvent);
};

private processFfiEvent = (ffiEvent: FfiEvent) => {
if (!this.localParticipant || !this.ffiHandle || !this.info) {
throw new Error('processFfiEvent called before connect');
}

if (ffiEvent.message.case == 'rpcMethodInvocation') {
Expand Down
Loading