diff --git a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts index 9ea87202..7a39f46a 100644 --- a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts +++ b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts @@ -189,7 +189,29 @@ export class SessionComponent implements OnInit, OnDestroy { async ngOnInit() { this.shouldDisconnectRoomWhenComponentIsDestroyed = true; - this.room = this.openviduService.getRoom(); + + // Check if room is available before proceeding + if (!this.openviduService.isRoomInitialized()) { + this.log.e('Room is not initialized when SessionComponent starts. This indicates a timing issue.'); + this.actionService.openDialog( + this.translateService.translate('ERRORS.SESSION'), + 'Room is not ready. Please ensure the token is properly configured.' + ); + return; + } + + // Get room instance + try { + this.room = this.openviduService.getRoom(); + this.log.d('Room successfully obtained for SessionComponent'); + } catch (error) { + this.log.e('Unexpected error getting room:', error); + this.actionService.openDialog( + this.translateService.translate('ERRORS.SESSION'), + 'Failed to get room instance: ' + (error?.message || error) + ); + return; + } // this.subscribeToCaptionLanguage(); this.subcribeToActiveSpeakersChanged(); diff --git a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts index a350ab74..b27e0543 100644 --- a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts +++ b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts @@ -64,6 +64,12 @@ export class OpenViduService { * @internal */ initRoom(): void { + // If room already exists, don't recreate it + if (this.room) { + this.log.d('Room already initialized, skipping re-initialization'); + return; + } + const videoDeviceId = this.deviceService.getCameraSelected()?.device ?? undefined; const audioDeviceId = this.deviceService.getMicrophoneSelected()?.device ?? undefined; const roomOptions: RoomOptions = { @@ -88,6 +94,7 @@ export class OpenViduService { disconnectOnPageLeave: true }; this.room = new Room(roomOptions); + this.log.d('Room initialized successfully'); } /** @@ -130,12 +137,20 @@ export class OpenViduService { */ getRoom(): Room { if (!this.room) { - this.log.e('Room is not initialized'); - throw new Error('Room is not initialized'); + this.log.e('Room is not initialized. Make sure token is set before accessing the room.'); + throw new Error('Room is not initialized. Make sure token is set before accessing the room.'); } return this.room; } + /** + * Checks if room is initialized without throwing an error + * @returns true if room is initialized, false otherwise + */ + isRoomInitialized(): boolean { + return !!this.room; + } + /** * Returns the room name */ @@ -171,6 +186,13 @@ export class OpenViduService { this.log.e('LiveKit URL is not defined. Please, check the livekitUrl parameter of the VideoConferenceComponent'); throw new Error('Livekit URL is not defined'); } + + // Initialize room if it doesn't exist yet + // This ensures that getRoom() won't fail if token is set before onTokenRequested + if (!this.room) { + this.log.d('Room not initialized yet, initializing room due to token assignment'); + this.initRoom(); + } // return this.room.prepareConnection(this.livekitUrl, this.livekitToken); }