Is there anyway for me to use livekit API to exten...
# ask-ai
f
Is there anyway for me to use livekit API to extend departure_timeout time? or to find out how much time left before the room closes during the departure_timeout phase? Otherwise, how should I support rejoining of room? whereby how do i check if the agent is still waiting in the room? before sending back the same livekit token to the frontend?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to better understand your situation so I can assist you more effectively. Could you share a bit more about how you're using the rejoining rooms feature and checking if an agent is still waiting? For example: • Are you trying to keep track of room activity from outside the system to manage rejoining? • Do you need to check if an agent is present in real-time or just at certain times? • What details are important for you when sending the same LiveKit token back to the frontend? Sources: livekit-api/CHANGELOG.md | cmd/lk/room.go | livekit-protocol/livekit/protocol/room.py | config-sample.yaml
f
That is what i am asking actually, what ways are there to perform the rejoining room feature? • I am trying to keep track of room activity from outside the system, so taht i can simply send the same token to the client • I need to check if agent is present at certain times, for example if client wish to rejoin the room • i think waht is important is that the room has not closed yet, so that the job can still continue from where the client has left off with all context
t
I'd love to help you implement a rejoining feature in LiveKit! Here are some key steps and things to keep in mind: • Automatic Reconnection: The LiveKit SDKs are designed to handle reconnections automatically if there are any network hiccups. They try to resume the session or do a full reconnect based on what the server says. • Room Activity Check: You can use the Server SDK to check if the room is still active before issuing a new token. This might involve looking at the participant count or the room’s metadata. • Token Management: It’s important to make sure tokens have enough TTL so reconnections can happen smoothly. If a token expires, you’ll want to generate a new one after confirming the room is still active. • Backend Logic: Adding some server-side logic to keep track of room activity and confirm the room is open helps ensure rejoining only happens when it should. • Event Handling: Listening for SDK events like
Reconnecting
and
Reconnected
is a great way to update your UI or keep users informed about what’s going on with their connection. If you’d like, I’m here to walk you through any of these steps in more detail or help with specific parts of your implementation. Just let me know! Sources: Connecting to LiveKit | LiveKit Docs | Sources/LiveKit/Core/Room+Engine.swift | src/room/Room.ts | livekit/src/rtc_engine/mod.rs
f
currently, the list all room that are active, is it real time? because there are instances where the room has closed already, but it still appears as active in the list.
t
I completely understand how important it is to have an accurate list of active rooms in LiveKit, and I want to share a little insight into why you might sometimes see closed rooms still showing up as active. Here’s what’s happening behind the scenes: • When a room closes, LiveKit waits for a departure timeout (which is usually 20 seconds) before it begins cleaning up. • After that, a sweeper process takes a bit more time—around 30–45 seconds—to finish the cleanup. • Sometimes, database delays or race conditions can cause the room’s status to stick around just a little longer. To help keep things smooth, especially if you’re scheduling back-to-back meetings with the same room name, a handy tip is to add a unique suffix like a timestamp or UUID to each room name. If you’d like, I’m here to help you set that up or answer any other questions you have! Sources: livekit-protocol/livekit/protocol/models.pyi | livekit/livekit_room.twirp.go | Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name
f
so the status is only updated after the clean up?
t
Thanks for reaching out! I wanted to let you know that the status of a room gets updated only after the cleanup process is fully complete. So, during the departure timeout phase and while the sweeper process finishes up—which can take about 30–45 seconds after the room is closed—you might still see the room listed as active. I completely understand how this might feel a bit confusing. If you're looking for more precise tracking of room activity, a helpful approach might be to add some extra logic on your backend to keep an eye on participant activity or use unique room identifiers. This can really help avoid any mix-ups. Please feel free to ask if you'd like some tips on how to set this up—I’m here to help!
f
can i do soemthing like this? @room.on("participant_disconnected") def on_participant_left(participant): print(f"Participant {participant.identity} left the room.") would this work whereby the room waits for this event? and i can update status in db to paused?
t
Absolutely, you can use the
participant_disconnected
event to keep your room status up-to-date in your database. The LiveKit SDKs support this event, which triggers whenever a participant leaves the room. Here’s a friendly guide on how to set it up: • First, register the event with
@room.on("participant_disconnected")
in the Python SDK. The callback will give you the
participant
object, containing helpful info like
sid
,
identity
, and
metadata
. • Within the callback, you can grab the participant and room details you need, then update your database accordingly—maybe marking the room as "paused" or adjusting the participant count. Here’s a simple example to illustrate:
Copy code
@room.on("participant_disconnected")
def on_participant_disconnected(participant):
    db.update_room_status(
        room_id=room.name,
        action="remove_participant",
        participant_id=participant.sid,
        timestamp=time.now()
    )
If you'd like, I’m here to help you integrate this with your specific database or backend framework—just let me know! Sources: stream_audio.py | src/room/Room.ts | lib/src/core/room.dart | livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.... | Runtime/Scripts/Room/Room.cs
f
where do i put this @room.on code at? Currently i do @session.on(event) but have never written for @room.on
t
When using the LiveKit Python SDK, it’s best to define your
@room.on
event handlers before you connect to the room. This way, you won’t miss any important events that might happen during the connection process. Here’s a simple example to help you get started:
Copy code
from livekit import Room

room = Room()

@room.on("participant_disconnected")
def on_participant_disconnected(participant):
    print(f"Participant {participant.identity} left the room.")

# Connect to the room after defining event handlers
room.connect("<ws://your-livekit-server>", "your-access-token")
If you have any questions or need a hand setting this up, please don’t hesitate to ask. I’m here to help! Sources: examples/face_landmark/face_landmark.py
f
"connected": Called when the room is successfully connected. ◦ Arguments: None • "disconnected": Called when the room is disconnected. ◦ Arguments:
reason
(DisconnectReason) • "reconnecting": Called when the room is attempting to reconnect. ◦ Arguments: None • "reconnected": Called when the room has successfully reconnected. ◦ Arguments: None Can u explain these room events? basically if participant were to leave the room and then rejoin, would they hit these events?
t
Thank you for reaching out! Let me walk you through the room events and how they work when a participant leaves and then rejoins: • "connected": This event happens when a participant first joins a room or after they’ve completely disconnected and then rejoined. So, if someone leaves and comes back, you’ll see this event again. • "disconnected": This triggers when a participant is fully disconnected from the room, whether they left on purpose or due to an issue that can’t be recovered. This covers situations where a participant leaves the room. • "reconnecting": This event fires when the connection to the server is temporarily interrupted—like if there are network hiccups—and the system is trying to reconnect. It’s important to know this is for short interruptions, not when someone fully leaves and comes back. • "reconnected": This happens when the reconnection is successful after a "reconnecting" event. The system then restores everything, including any published tracks. So, to sum it up: if a participant just briefly loses connection, you’ll see "reconnecting" followed by "reconnected." But if they fully leave and then rejoin, you’ll notice a "disconnected" event followed by "connected." Please feel free to ask if anything isn’t clear or if you want to dive deeper—I’m here to help! Sources: src/room/Room.ts | Connecting to LiveKit | LiveKit Docs