adorable-portugal-55771
08/22/2025, 10:11 AMtall-airline-16756
08/22/2025, 10:11 AMtall-airline-16756
08/22/2025, 10:13 AMAgentDispatchService to explicitly redispatch an agent. Just make sure to set an agent_name in the WorkerOptions to turn off automatic dispatch.
2. Session Replacement Pattern: Another option is to replace the session during reconnections. This means ending the current session and starting a new one with the same agent and context.
3. Participant Connection Event Handling: Keep an eye on participant connection events and handle reconnections by reinitializing the agent or session when needed.
4. Token-Based Agent Dispatch: You can also set up agent dispatch when generating the token to make sure an agent is ready as soon as the user reconnects.
Each method works best depending on things like whether you want to keep the conversation history, how fast the agent should be ready, and if you prefer automatic or explicit dispatch. Just let me know if you'd like me to walk you through the steps for any of these options—I’m here to help!adorable-portugal-55771
08/22/2025, 10:14 AMtall-airline-16756
08/22/2025, 10:15 AM// In your token generation endpoint or function
import { AccessToken, RoomConfiguration, RoomAgentDispatch } from 'livekit-server-sdk';
async function createTokenWithAgentDispatch(roomName, agentName, participantName) {
const at = new AccessToken();
at.identity = participantName;
at.addGrant({ roomJoin: true, room: roomName });
// Configure agent dispatch in the room configuration
at.roomConfig = new RoomConfiguration({
agents: [
new RoomAgentDispatch({
agentName: agentName,
metadata: '{"mydata": "myvalue"}', // Optional metadata
}),
],
});
return await at.toJwt();
}
Approach 2: Explicit API-Based Dispatch
If you prefer, you can also create explicit dispatches using the AgentDispatchClient directly from your React app’s backend.
import { AgentDispatchClient } from 'livekit-server-sdk';
async function createExplicitDispatch(roomName, agentName) {
const agentDispatchClient = new AgentDispatchClient(process.env.LIVEKIT_URL);
// Create a dispatch for the specific agent to join the room
const dispatch = await agentDispatchClient.createDispatch(roomName, agentName, {
metadata: '{"mydata": "myvalue"}',
});
console.log('created dispatch', dispatch);
return dispatch;
}
If you want me to walk you through any part of this or if you have more questions, just give me a shout. I’m here to help!