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/slick-cougars-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/components-react': patch
---

Make useSession wait for agent only if an agent was dispatched
17 changes: 13 additions & 4 deletions packages/react/src/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TokenSourceFixed,
TokenSourceFetchOptions,
RoomConnectOptions,
decodeTokenPayload,
} from 'livekit-client';
import { EventEmitter } from 'events';

Expand Down Expand Up @@ -451,12 +452,18 @@ export function useSession(
};
signal?.addEventListener('abort', onSignalAbort);

let tokenDispatchesAgent = false;
await Promise.all([
// FIXME: swap the below line in once the new `livekit-client` changes are published
// room.connect(tokenSource, { tokenSourceOptions }),
tokenSourceFetch().then(({ serverUrl, participantToken }) =>
room.connect(serverUrl, participantToken, roomConnectOptions),
),
tokenSourceFetch().then(({ serverUrl, participantToken }) => {
const participantTokenPayload = decodeTokenPayload(participantToken);
const participantTokenAgentDispatchCount =
participantTokenPayload.roomConfig?.agents?.length ?? 0;
tokenDispatchesAgent = participantTokenAgentDispatchCount > 0;

return room.connect(serverUrl, participantToken, roomConnectOptions);
}),

// Start microphone (with preconnect buffer) by default
tracks.microphone?.enabled
Expand All @@ -469,7 +476,9 @@ export function useSession(
]);

await waitUntilConnected(signal);

Choose a reason for hiding this comment

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

question

without knowing enough context on the code, I wonder if we shoul do both waitUntilConnected() and await agent.waitUntilAvailable(signal); in some cases ?

or it should be something like
if (tokenDispatchesAgent && agent) {
await agent.waitUntilAvailable(signal);
} else {
await waitUntilConnected(signal);
}

also, should you guard agent ?

Copy link
Contributor Author

@1egoman 1egoman Oct 28, 2025

Choose a reason for hiding this comment

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

or it should be something like

waitUntilConnected will block until a the underlying room is connected, and agent.waitUntilAvailable will block until the agent has connected. One is dependent on the other, and while I guess technically the agent could never connect until the room connects first, this should always happen in a sequential fashion so I'm not sure that it really matters that much. So I guess I don't have a strong preference given both should behave identically, but maybe lean slightly towards what I have now.

also, should you guard agent ?

No, this isn't required - agent is always set, but its internal state changes as the agent goes through the connection lifecycle.

await agent.waitUntilAvailable(signal);
if (tokenDispatchesAgent) {
await agent.waitUntilAvailable(signal);
}

signal?.removeEventListener('abort', onSignalAbort);
},
Expand Down
62 changes: 34 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ packages:
- 'tooling/*'

catalog:
livekit-client: ^2.15.8
livekit-client: ^2.15.14
"@livekit/protocol": ^1.42.0