```livekit_api = get_livekit_api() request...
# ask-ai
b
Copy code
livekit_api = get_livekit_api()
        request = ListParticipantsRequest(room=ctx.room.name)
        async with livekit_api:
            total_participants = await livekit_api.room.list_participants(request)
            participants = total_participants.participants
            <http://logger.info|logger.info>("Total participants in room: %s", len(participants))
Occasionally, a participant disconnects before the call officially starts, which results in the room being automatically deleted by LiveKit. When that happens, calling
list_participants
raises a
TwirpError('not_found', 'requested room does not exist')
. Is there a way to check if the room exists before calling
list_participants
, or a recommended pattern to handle this scenario more gracefully?
t
Thanks for reaching out, Ahsan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To handle the scenario where a room might not exist before calling list_participants, you can use the listRooms method to check for the room's existence. Here's an example pattern:
Copy code
const rooms = await roomServiceClient.listRooms([roomName]);
if (rooms.length > 0) {
  // Room exists, now you can list participants
  const participants = await roomServiceClient.listParticipants(roomName);
}
The listRooms method accepts an optional array of room names to filter the results and returns an array of Room objects. If the array is empty, it means the room doesn’t exist or is not active. This approach can help you handle the situation more gracefully. Sources: RoomServiceClient | LiveKit JS Server SDK - v2.9.7
In the Python LiveKit SDK, you can use the list_rooms method to check if a room exists before calling list_participants. Here's an example pattern:
Copy code
from livekit import ListRoomsRequest

# Create a request to filter by room name
request = ListRoomsRequest(names=[room_name])

# Call the list_rooms method
response = await livekit_api.room.list_rooms(request)

# Check if the room exists
if response.rooms:
    # Room exists, now you can list participants
    participants_response = await livekit_api.room.list_participants(room_name)
This approach ensures that you only call list_participants for rooms that are active. Sources: livekit.api.room_service API documentation