I want a room to be deleted automatically if it ha...
# ask-ai
c
I want a room to be deleted automatically if it has only one participant (local or remote) for more than X seconds. Which parameter controls this? How can I set it. Show code example in python.
t
Thanks for reaching out, Yousri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The LiveKit configuration includes
empty_timeout
and
departure_timeout
settings for rooms: • `empty_timeout`: seconds to keep a room open if no one joins • `departure_timeout`: seconds to keep a room open after everyone leaves These can be set when creating a room using the
CreateRoomRequest
. However, these do not cover the exact scenario of automatically deleting a room if it has only one participant for more than X seconds. To handle that scenario, you would need to manually monitor the participant count using the LiveKit API and delete the room when the condition is met. Here is a Python example combining both parts:
Copy code
from livekit import api
from livekit.protocol.room import CreateRoomRequest
import asyncio

# Initialize LiveKit API client
lkapi = api.LiveKitAPI(
    "YOUR_LIVEKIT_URL",
    "YOUR_API_KEY",
    "YOUR_API_SECRET",
)

# Create room with timeouts
create_room_request = CreateRoomRequest(
    name="my_room",
    empty_timeout=30,  # in seconds
    departure_timeout=30,  # in seconds
)

room = lkapi.room.create_room(create_room_request)

# Monitor room for single participant condition
async def monitor_room(lkapi, room_name, single_participant_timeout=30):
    while True:
        participants = await lkapi.room.list_participants(
            api.ListParticipantsRequest(room=room_name)
        )

        if len(participants.participants) == 1:
            await asyncio.sleep(single_participant_timeout)
            participants = await lkapi.room.list_participants(
                api.ListParticipantsRequest(room=room_name)
            )

            if len(participants.participants) == 1:
                await lkapi.room.delete_room(
                    api.DeleteRoomRequest(room=room_name)
                )
                break

        await asyncio.sleep(1)

# Run the monitoring task
asyncio.run(monitor_room(lkapi, "my_room"))
This example shows how to set timeouts on room creation and implement your own logic to automatically delete the room if it stays with only one participant for a specified timeout. Let me know if you need details on any part of this setup. Sources: livekit/livekit_room.pb.go