Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prepareConnection option to LiveKitRoom #955

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/cool-nails-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-react": minor
---

Add prepareConnection option to LiveKitRoom (enabled by default)
1 change: 1 addition & 0 deletions packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export interface LiveKitRoomProps extends Omit<React_2.HTMLAttributes<HTMLDivEle
// (undocumented)
onMediaDeviceFailure?: (failure?: MediaDeviceFailure) => void;
options?: RoomOptions;
prepareConnection?: boolean;
room?: Room;
screen?: ScreenShareCaptureOptions | boolean;
serverUrl: string | undefined;
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/components/LiveKitRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export interface LiveKitRoomProps extends Omit<React.HTMLAttributes<HTMLDivEleme

simulateParticipants?: number | undefined;

/**
* If set to `true` (default) an initial HTTP request will be sent to the serverURL even before connecting to prewarm the connection
*/
prepareConnection?: boolean;

/**
* @internal
*/
Expand Down
19 changes: 15 additions & 4 deletions packages/react/src/hooks/useLiveKitRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultRoomProps: Partial<LiveKitRoomProps> = {
connect: true,
audio: false,
video: false,
prepareConnection: true,
};

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ export function useLiveKitRoom<T extends HTMLElement>(
onMediaDeviceFailure,
onEncryptionError,
simulateParticipants,
prepareConnection,
...rest
} = { ...defaultRoomProps, ...props };
if (options && passedRoom) {
Expand All @@ -59,6 +61,13 @@ export function useLiveKitRoom<T extends HTMLElement>(
setRoom(passedRoom ?? new Room(options));
}, [passedRoom]);

const prewarm = React.useMemo(() => {
if (room && serverUrl && prepareConnection && token) {
return room.prepareConnection(serverUrl, token);
}
return new Promise<void>((resolve) => resolve(undefined));
}, [serverUrl, prepareConnection, token, room]);

const htmlProps = React.useMemo(() => {
const { className } = setupLiveKitRoom();
return mergeProps(rest, { className }) as HTMLAttributes<T>;
Expand Down Expand Up @@ -126,10 +135,12 @@ export function useLiveKitRoom<T extends HTMLElement>(
}
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();
Expand Down