When the conversation is in the end, how to discon...
# ask-ai
h
When the conversation is in the end, how to disconnect user and delete the room, so that user can start a new one? If the room is not deleted, the agent for the new conversation is not dispatched in my use case.
t
Thanks for reaching out, Pavel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To disconnect a user and delete a room in LiveKit, you can follow these steps: 1. Disconnecting everyone and deleting the room: Use the server API's
deleteRoom
method to end the session for all participants and remove the room from the server. Here's an example: `from livekit import api async def entrypoint(ctx: JobContext): api_client = api.LiveKitAPI( os.getenv("LIVEKIT_URL"), os.getenv("LIVEKIT_API_KEY"), os.getenv("LIVEKIT_API_SECRET"), ) await api_client.room.delete_room(api.DeleteRoomRequest( room=ctx.job.room.name, ))`This will send a "Disconnected" room event to all participants and delete the room. 2. Disconnecting the agent only: If you only want to disconnect the agent while keeping the room active for other participants, you can use the
ctx.shutdown()
method:
Copy code
async def entrypoint(ctx: JobContext):
    # Disconnect from the room
    ctx.shutdown(reason="Session ended")
Using the first approach ensures the room is deleted, allowing a new conversation to start with a fresh room and agent dispatch. Sources: Inside a session | LiveKit Docs
h
How long it takes until the room is deleted? It happened to me that when I delete a room and immediately create a new one, the agent is not dispatched.
t
Hey there! When you use the
deleteRoom
API in LiveKit, the room gets zapped from the server's memory right away. Any participants in the room will be automatically disconnected with a "ROOM_DELETED" heads-up. I totally get that it can be frustrating if you're seeing delays with agent dispatching after creating a new room. This might be happening because of some behind-the-scenes processing. No worries, though! You might want to double-check that the room deletion is completely done before spinning up a new one, or maybe add a tiny pause between actions to give the system a chance to catch its breath. Sources: Connecting to LiveKit | LiveKit Docs | GitHub::livekit::livekit_composite | livekit/livekit-cli/cmd/lk/room.go