I remove a participant through the following code ...
# ask-ai
c
I remove a participant through the following code from a Room, this is basically used when the agent hangsup the phone connection, see code below. How can I listen to an event when this happens and which event is that? await livekit_api.room.remove_participant(api.RoomParticipantIdentity( room=room_name, identity=participant_identity ))
t
Thanks for reaching out, Vahid. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When a participant is removed using the
RemoveParticipant
API in LiveKit, a Disconnected event is emitted. The reason for the disconnection in this case will be
PARTICIPANT_REMOVED
. You can listen for the Disconnected event and check the reason to handle this scenario in your application. Sources: Connecting to LiveKit | LiveKit Docs | DisconnectReason
c
Can you tell me what is the best and correct approach in closing the room and having participant removed so conversation is hung up?
t
The best and correct approach to close a room and remove all participants in LiveKit is to use the
DeleteRoom
method from the RoomService API. This method terminates the room entirely and causes all participants to leave at once, ensuring the conversation is effectively hung up. Here is an example implementation in Go:
Copy code
import (
    lksdk "<http://github.com/livekit/server-sdk-go/v2|github.com/livekit/server-sdk-go/v2>"
    livekit "<http://github.com/livekit/protocol/livekit|github.com/livekit/protocol/livekit>"
)

func main() {
    hostURL := "host-url"  // ex: <https://project-123456.livekit.cloud>
    apiKey := "api-key"
    apiSecret := "api-secret"
    
    roomId := "your-room-id"
    
    roomClient := lksdk.NewRoomServiceClient(hostURL, apiKey, apiSecret)
    
    // terminate a room and cause participants to leave
    roomClient.DeleteRoom(context.Background(), &livekit.DeleteRoomRequest{
        Room: roomId,
    })
}
This approach is more efficient than removing participants one by one and ensures the room is properly closed. Sources: lksdk package - github.com/livekit/server-sdk-go/v2 - Go Packages | lksdk package - github.com/livekit/server-sdk-go/v2 - Go Packages | lksdk package - github.com/livekit/server-sdk-go/v2 - Go Packages