diff --git a/.changeset/cool-nails-exist.md b/.changeset/cool-nails-exist.md new file mode 100644 index 000000000..647d2e27f --- /dev/null +++ b/.changeset/cool-nails-exist.md @@ -0,0 +1,5 @@ +--- +"@livekit/components-react": minor +--- + +Add prepareConnection option to LiveKitRoom (enabled by default) diff --git a/packages/react/etc/components-react.api.md b/packages/react/etc/components-react.api.md index bbd2c00cd..0547ee89d 100644 --- a/packages/react/etc/components-react.api.md +++ b/packages/react/etc/components-react.api.md @@ -344,6 +344,7 @@ export interface LiveKitRoomProps extends Omit void; options?: RoomOptions; + prepareConnection?: boolean; room?: Room; screen?: ScreenShareCaptureOptions | boolean; serverUrl: string | undefined; diff --git a/packages/react/src/components/LiveKitRoom.tsx b/packages/react/src/components/LiveKitRoom.tsx index 71f89415e..5834e9270 100644 --- a/packages/react/src/components/LiveKitRoom.tsx +++ b/packages/react/src/components/LiveKitRoom.tsx @@ -77,6 +77,11 @@ export interface LiveKitRoomProps extends Omit = { connect: true, audio: false, video: false, + prepareConnection: true, }; /** @@ -45,6 +46,7 @@ export function useLiveKitRoom( onMediaDeviceFailure, onEncryptionError, simulateParticipants, + prepareConnection, ...rest } = { ...defaultRoomProps, ...props }; if (options && passedRoom) { @@ -59,6 +61,13 @@ export function useLiveKitRoom( setRoom(passedRoom ?? new Room(options)); }, [passedRoom]); + const prewarm = React.useMemo(() => { + if (room && serverUrl && prepareConnection && token) { + return room.prepareConnection(serverUrl, token); + } + return new Promise((resolve) => resolve(undefined)); + }, [serverUrl, prepareConnection, token, room]); + const htmlProps = React.useMemo(() => { const { className } = setupLiveKitRoom(); return mergeProps(rest, { className }) as HTMLAttributes; @@ -126,10 +135,12 @@ export function useLiveKitRoom( } if (connect) { log.debug('connecting'); - room.connect(serverUrl, token, connectOptions).catch((e) => { - log.warn(e); - onError?.(e as Error); - }); + prewarm.then(() => + room.connect(serverUrl, token, connectOptions).catch((e) => { + log.warn(e); + onError?.(e as Error); + }), + ); } else { log.debug('disconnecting because connect is false'); room.disconnect();