I am using the go server sdk - dispatchClient.Cre...
# ask-ai
f
I am using the go server sdk - dispatchClient.CreateDispatch for dispatching agent. I want to wait after calling this, Till the agent join process has either succeeded or failed, can you give me the code for that...
t
Thanks for reaching out, Tushar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to wait for the agent join process to either succeed or fail after calling
dispatchClient.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:
Copy code
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`:
Copy code
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:
Copy code
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:
Copy code
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