Is there a way to check how many concurrent users ...
# ask-ai
c
Is there a way to check how many concurrent users are currently connected to LiveKit? I want to be set up some logic such that even when there are the max number of concurrent sessions, we have a waiting screen to allow users to come back and have a cue to start using and connect to the LiveKit agent in this scenario.
t
Thanks for reaching out, Thomas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to understand a bit more about how you're using LiveKit right now. Could you share: • Whether you're using LiveKit Cloud or running a self-hosted LiveKit server? • If you have a particular maximum number of concurrent sessions you're aiming for? • Are you hoping to monitor user connections programmatically, or would you prefer using a dashboard? Knowing these details will really help me guide you in the best way possible.
c
1. i'm using livekit cloud 2. i'm not looking for a particular maximum number of concurrent sessions but rather wanting to see what is the current number of live concurrent sessions. 3. wanting to check the live number of concurrent sessions programmatically
t
I'd be happy to help you check the number of concurrent users connected to LiveKit Cloud programmatically! Here are a couple of ways you can do this: • Analytics API: This API lets you list active sessions and get details about participants. To get started, you’ll want to generate an access token with the
roomList
grant, then use the
/api/v1/sessions
endpoint to pull session data, including how many participants are connected. • Server SDKs: If you prefer using SDKs like Python, Node.js, or Rust, you can connect to the RoomService API to list participants in a room. For instance, with Python, the
livekit-api
library makes it straightforward to fetch participant details. Just a quick heads-up: the Analytics API is available for customers on the Scale plan or higher. If you’d like to dive deeper, the Analytics API documentation and Server SDKs documentation are great resources to explore. Sources: Analytics API | LiveKit Docs | examples/multiple_connections.py | src/pages/api/room_info/[room].ts | README.md | README.md
c
I'm trying to call the /api/v1/sessions endpoint using the roomList grant with the following code
Copy code
const token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
    token.addGrant({ roomList: true });

    const jwt = await token.toJwt();

    const endpoint = `<https://cloud-api.livekit.io/api/project/${PROJECT_ID}/sessions/>`;

    <http://log.info|log.info>("Checking LiveKit sessions", { endpoint });

    const response = await fetch(endpoint, {
      method: "GET",
      headers: {
        Authorization: `Bearer ${jwt}`,
        "Content-Type": "application/json",
      },
    });
but i'm getting a 401 unauthorized error. what is the issue?
t
I'm sorry to hear you're running into the 401 Unauthorized error when calling the
/api/v1/sessions
endpoint. This usually happens for a few common reasons, so let’s walk through them together: • Token Issues: It’s important to make sure your token includes the
roomList
grant and is generated correctly. A handy way to check this is by decoding your token using a tool like jwt.io, which lets you see the token’s structure and claims. • Clock Drift: Since JWT tokens are time-sensitive, if the system clock that generates the token isn’t synced with LiveKit’s servers, the token might get rejected. Double-checking your system time can really help here. • API Key/Secret Issues: Please verify that your
LIVEKIT_API_KEY
and
LIVEKIT_SECRET_KEY
are correctly formatted and don’t have any extra spaces or unexpected characters. • Project ID Mismatch: Make sure the
PROJECT_ID
in your API URL matches the project linked to your API key and secret. This can sometimes be an easy thing to overlook. Here are a few steps you can try to troubleshoot further: 1. Decode your JWT and confirm it includes the
roomList
grant. 2. Check the token’s
nbf
(not before) and
exp
(expiration) claims to ensure it’s valid at the current time. 3. Try making the API call with minimal parameters using a tool like
curl
to rule out any malformed requests. If you’re still stuck after these steps, please don’t hesitate to reach out—I’m here to help you get this sorted! Sources: Analytics API | LiveKit Docs | auth/grants.go | Authentication | LiveKit Docs