future-umbrella-45124
09/08/2025, 11:45 AMtall-airline-16756
09/08/2025, 11:45 AMtall-airline-16756
09/08/2025, 11:46 AMdispatchClient.CreateDispatch in the Go Server SDK, a good way is to keep an eye on the room's participant connection events. Here’s a simple example to help you get started:
1. Set up a Room and Callback:
room := lksdk.NewRoom(&lksdk.RoomCallback{
OnParticipantConnected: func(rp *lksdk.RemoteParticipant) {
if rp.Kind() == lksdk.ParticipantAgent {
log.Printf("Agent successfully joined room: %s", rp.Identity())
// You can set a flag or signal a channel here to indicate success
}
},
OnParticipantDisconnected: func(rp *lksdk.RemoteParticipant) {
log.Printf("Participant disconnected: %s", rp.Identity())
// Optionally, handle agent disconnection or failure here
},
})
2. Call `CreateDispatch`:
dispatchClient := lksdk.NewAgentDispatchServiceClient(url, apiKey, secretKey)
req := &livekit.CreateAgentDispatchRequest{
Room: roomName,
AgentName: agentName,
}
_, err := dispatchClient.CreateDispatch(context.Background(), req)
if err != nil {
log.Printf("Failed to create dispatch: %v", err)
return err
}
log.Printf("Dispatch request sent for agent %s to room %s", agentName, roomName)
3. Wait for Agent Join with Timeout:
agentJoined := false
var mu sync.Mutex
room.Callback.OnParticipantConnected = func(rp *lksdk.RemoteParticipant) {
if rp.Kind() == lksdk.ParticipantAgent {
mu.Lock()
agentJoined = true
mu.Unlock()
}
}
// Now, wait for the agent to join or timeout after 30 seconds
timeout := time.After(30 * time.Second)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
mu.Lock()
joined := agentJoined
mu.Unlock()
if joined {
log.Printf("Agent join succeeded")
break
}
select {
case <-timeout:
return fmt.Errorf("agent failed to join within timeout")
case <-ticker.C:
// Keep waiting
}
}
If you prefer, here’s an alternative using a channel which can be a bit more efficient:
agentJoinedCh := make(chan struct{}, 1)
room.Callback.OnParticipantConnected = func(rp *lksdk.RemoteParticipant) {
if rp.Kind() == lksdk.ParticipantAgent {
select {
case agentJoinedCh <- struct{}{}:
default:
}
}
}
select {
case <-agentJoinedCh:
log.Printf("Agent successfully joined")
case <-time.After(30 * time.Second):
return fmt.Errorf("agent failed to join within timeout")
}
This way, you’ll be able to tell right away when the agent joins successfully, or handle the situation gracefully if it doesn’t happen within your set timeout. Hope this helps!
Sources: room.go | livekit/livekit_agent_dispatch.pb.go | cmd/lk/dispatch.go | Web and mobile frontends | LiveKit Docs | Web and mobile frontends | LiveKit Docs