how to stop session i.e. close room and disconnect...
# ask-ai
a
how to stop session i.e. close room and disconnect everyone after agent respond?
t
Thanks for reaching out, affandy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to close a room and disconnect everyone after an agent responds, you can use the LiveKit server API's
deleteRoom
method. This will wrap up the session for all participants and take down the room. Here’s a little example to show you how it works:
Copy code
from livekit import api

async def entrypoint(ctx: JobContext):
    # Perform the agent's task and generate a response
    # ... (agent's logic here) ...

    # Disconnect all participants and close the room
    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)
    )
So, in this example: • The agent does its thing and creates a response. • Then, the
deleteRoom
method is called to close the room and disconnect everyone. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
a
can I use
ctx.shutdown()
? what's the difference with your solution?
t
Hey there! Just wanted to help clarify the difference between
ctx.shutdown()
and the
deleteRoom
API, since they serve different purposes: •
ctx.shutdown()
: This one just disconnects the agent from the room, but the room stays open for everyone else. So, if the agent’s part is done but others want to keep chatting, this is the way to go. •
deleteRoom
API
: This one shuts down the whole room, kicking everyone out and removing it completely. Perfect when you want to wrap things up for everyone. So, if you want the room to keep going after the agent leaves,
ctx.shutdown()
is your friend. But if it’s time to close up shop and disconnect everyone, then
deleteRoom
is the right call. Feel free to ask if you want me to explain anything else or help out more! Sources: Job lifecycle | LiveKit Docs | Connecting to LiveKit | LiveKit Docs
a
when
@session.on("close")
will be executed?
t
Hey there! Just wanted to share when the
@session.on("close")
event kicks in: • User-Initiated Closure: This happens when the user decides to end the conversation. • Manual Closure: When you call
session.aclose()
in your code. • Room Deletion: If the room gets deleted, everyone gets disconnected. • Unrecoverable Errors: When something goes wrong that the session can’t recover from. When this event fires, you get a
CloseEvent
object that gives you details like any error info and why the session closed (for example,
USER_INITIATED
or
ROOM_DELETED
). Here’s a quick example:
Copy code
@session.on("close")
def on_close(ev: CloseEvent):
    print(f"Session closed. Reason: {ev.reason}")
    # You can add any cleanup or logging you need here
This way, you can make sure everything wraps up nicely when a session ends. If you want to chat more about it or need help, just let me know!
a
since you are mentioned it, please explain what's the difference between session.aclose, ctx.shutdown, and deleteRoom?
t
Let me walk you through the differences between
session.aclose()
,
ctx.shutdown()
, and
deleteRoom
so it’s a bit clearer: •
session.aclose()
: This gently closes the agent's session. It only affects the agent’s connection, while the room and other participants keep going. It’s perfect when the agent’s job is done but the room should stay open. •
ctx.shutdown()
: This disconnects the agent from the room but leaves the room active for everyone else. Handy when the agent needs to step away but the session should keep running for others. •
deleteRoom
: This one deletes the whole room, kicking everyone out and removing it from the server. Use this when you want to end the session completely for everyone involved. You can mix and match these depending on what you need. For example, you might call
session.aclose()
first and then
deleteRoom
to fully shut things down. If you want me to clarify anything else, just let me know—I’m here to help! Sources: flows/declarative_flow.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs
👍 1