is there a way to get all the room names that are ...
# ask-ai
b
is there a way to get all the room names that are active under my LK project
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to see all the active room names in your LiveKit project, you can easily do that using the
list_rooms
method from the RoomService client. It’ll give you a list of all the rooms currently active in your project. Here’s a simple example in Python using the LiveKit SDK:
Copy code
from livekit import api
from livekit.protocol.room import ListRoomsRequest

async def list_active_rooms():
    lkapi = api.LiveKitAPI()
    request = ListRoomsRequest(names=[])
    rooms_response = await lkapi.room.list_rooms(request)
    room_names = [room.name for room in rooms_response.rooms]
    return room_names

# Example usage:
# rooms = await list_active_rooms()
# print(rooms)
This little snippet sets up the RoomService client, calls
list_rooms
, and pulls out the names of all the active rooms for you. And if there aren’t any active rooms right now, you’ll just get back an empty list—nice and simple! Sources: livekit/livekit_egress.pb.go | examples/api.py | packages/livekit-server-sdk/src/RoomServiceClient.ts
b
i want to have a record of the chat of all the rooms under my livekit project
t
LiveKit doesn’t come with built-in storage for chat messages, but no worries—you can set up your own custom solution to capture and save messages from all the rooms in your project. Here’s a simple way to do it: 1. Capture Chat Messages: Use the
registerTextStreamHandler
method to listen to the
lk.chat
topic and grab messages as they come in. For example:
Copy code
// Listen to chat messages and store them
room.registerTextStreamHandler('lk.chat', async (reader, participantInfo) => {
  const { id, timestamp } = <http://reader.info|reader.info>;

  for await (const chunk of reader) {
    const message = {
      id,
      timestamp,
      roomName: room.name,
      sender: participantInfo?.identity,
      content: chunk,
    };

    // Store the message in your database
    await storeMessageInDatabase(message);
  }
});
1. Store Messages: Save those captured messages in a database you like—Redis, PostgreSQL, MongoDB, whatever fits your setup—with details like message ID, timestamp, room name, sender, and content. 2. Retrieve Messages: Set up an API endpoint to pull messages from your database, filtering by room name or whatever criteria you need. Here’s an example:
Copy code
// Example API route to fetch chat history
export async function GET(request: Request) {
  const { roomName } = await request.json();

  try {
    const messages = await fetchMessagesFromDatabase(roomName);
    return NextResponse.json(messages);
  } catch (error) {
    return NextResponse.json(
      { error: 'Failed to fetch messages' },
      { status: 500 }
    );
  }
}
With this setup, you’ll have a handy record of all chat messages across your LiveKit rooms. If you want to chat more about this or need a hand, just let me know—I’m here to help! Sources: Sending text | LiveKit Docs | examples/demo/demo.ts | client/app/actions.ts | complex-agents/role-playing/role_playing_frontend/hooks/useChatAndTranscrip...