We’re using LiveKit Cloud + Analytics API and want...
# ask-ai
r
We’re using LiveKit Cloud + Analytics API and want to implement usage-based quotas. Our requirement: • Each location in our platform gets a pool of “connection minutes.” • A location can have multiple sessions running concurrently. • We need to track connection minutes in real time, attributed per session and per location. • Users should be able to see on the fly which session consumed how many minutes. Has anyone here built something similar — a real-time session-minute tracker — on top of LiveKit Cloud?
t
Thanks for reaching out, Shreyansh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you set up a real-time session-minute tracker and usage-based quotas! To make sure I provide the best guidance, could you please share a bit more about your current setup? • Are you already using the LiveKit Analytics API to track session data, or is this something new you're looking to implement? • Would you like some help getting started with the Analytics API, or are you more interested in tips on calculating and displaying connection minutes? • How do you currently manage locations and sessions on your platform? For instance, do you have unique identifiers for each?
r
Are you already using the LiveKit Analytics API to track session data, or is this something new you're looking to implement? This is something new we’re looking to implement. Would you like some help getting started with the Analytics API, or are you more interested in tips on calculating and displaying connection minutes? We’re more interested in tips on calculating and displaying accurate connection minutes in real time. How do you currently manage locations and sessions on your platform? For instance, do you have unique identifiers for each? Yes, we have a one-to-many mapping where one location (with its own ID) can have multiple sessions with different IDs. Also, in our setup, each room is used only once.
t
I'd love to help you implement a real-time session-minute tracker and usage-based quotas using LiveKit Cloud and the Analytics API. Here’s a friendly step-by-step guide to get you started: 1. Ensure Access: First, please check that your project is on the LiveKit Cloud Scale plan or higher, as this is needed to access the Analytics API. 2. Retrieve Data: Next, you’ll want to use the Analytics API to gather session and participant data. The key endpoints you’ll find useful are: •
GET /sessions/
for an overview of session data. •
GET /sessions/{SESSION_ID}
for detailed participant info, including
joinedAt
and
leftAt
timestamps. 1. Calculate Connection Minutes: For each participant, calculate how long they were connected by using the
joinedAt
and
leftAt
times. For participants still active, just use the current time as the endpoint. Remember to exclude non-billable participants like ingress and egress users. 2. Group by Location: You can use the
AnalyticsClientMeta
fields (like region and country) to assign connection minutes to specific locations, which is super helpful for detailed insights. 3. Enable Real-Time Updates: To keep everything up to date, try polling the session details endpoint regularly (for example, every 60 seconds) so your data reflects real-time changes. 4. Display in a Dashboard: Finally, consider using a frontend framework like Bootstrap along with Chart.js to build a dashboard that updates dynamically. This way, you can easily track metrics like total connection minutes and usage by location. If you’d like, I’m here to provide more details or even share some code examples for any of these steps—just let me know! I’m happy to help you every step of the way. Sources: LiveKit Pricing | Analytics API | LiveKit Docs | livekit/livekit_analytics.pb.go | Understanding LiveKit Cloud Pricing | Say hello to improved Cloud analytics
r
From Locations do not get confused, it is something we use in our organization as an entity, it is not related to any geographical location
t
Thank you so much for clarifying that by "locations," you mean organizational entities rather than geographical ones. That really helps me understand your situation better! The steps I shared earlier still apply, but instead of grouping connection minutes by geographic data, you can group them by your organizational location IDs. Here’s how you can do this: • Make sure each session is tagged with the correct location ID within your system. • When you pull session and participant data through the Analytics API, link the session data to your location IDs. • Then, calculate and show the connection minutes for each location by adding up the minutes from all sessions tied to that location ID. If you’d like, I’m here to help you walk through setting up this mapping or anything else you might need. Just let me know!
r
will it be real time?? will session API show on going sessions as well?
t
Absolutely, the LiveKit Analytics API is great for showing ongoing sessions in real time. It provides valuable data about live sessions, including metrics like
createdAt
,
lastActive
,
numParticipants
, and more. You’ll find active sessions in the API response, and you can spot them by looking at fields such as
lastActive
or other activity indicators. That said, the API works on a polling model, so you’ll want to query the
/sessions
endpoint regularly to get updates. While it’s designed to help with real-time monitoring, there might be a tiny delay due to how the data is processed. For smooth tracking, setting up a polling interval—say, every 60 seconds—can help you fetch and show the latest session info consistently. If you’d like, I’m here to help you set up the polling or walk you through understanding the API responses. Just let me know! Sources: Say hello to improved Cloud analytics | Analytics API | LiveKit Docs
r
can u show me how to calculate the minutes in detail here??
t
Let's walk through how to calculate connection minutes step-by-step, so it feels manageable: 1. Fetch Participant Data: Start by using the Analytics API to get session details for each participant. The important fields you'll want to look at are: • `joinedAt`: This tells you when the participant joined the session. • `leftAt`: This shows when they left. If they’re still connected, this will be empty. 1. Calculate Duration for Each Participant: For every participant: • If
leftAt
is there, you can find the duration by:
Duration = (leftAt - joinedAt)
• If
leftAt
isn’t there (meaning they’re still active), just use the current time:
Duration = (currentTime - joinedAt)
1. Convert to Minutes: Since the timestamps are in milliseconds, divide by 60,000 to get minutes:
ConnectionMinutes = Duration / 60000
2. Sum Up for the Session: Add all participants’ connection minutes to find the total for that session. 3. Group by Location: If you want to group by location, simply add up the connection minutes for all sessions linked to a specific location ID. If anything feels unclear or if you'd like me to share a code example to make this even easier, just let me know—I’m here to help!
r
Instead of relying on the
GET /sessions
API, which can get pretty bloated, why not call
list_rooms
from RoomService every minute to fetch the active rooms? You could then take each session ID from that list and push it into the queue for processing.
t
Absolutely! You can use the
list_rooms
method from RoomService to fetch active rooms and work with session IDs for real-time tracking. This can actually be a smoother way compared to relying solely on the
GET /sessions
API, especially when you’re focusing on active rooms. Here’s a friendly step-by-step to help you get started: 1. Fetch Active Rooms: Use the
list_rooms
method to get a list of rooms that are currently active. Just a heads-up, you’ll need the
roomList
permission for this. 2. Retrieve Participant Data: For each active room, call
list_participants
to gather details about all participants, including their session IDs (SIDs). 3. Process Session IDs: You can then push these session IDs into a queue to handle things like calculating connection minutes or grouping participants by location. Here’s a simple example in Python to illustrate:
Copy code
rooms = await lkapi.room.list_rooms(ListRoomsRequest())
for room in rooms.rooms:
    participants = await lkapi.room.list_participants(ListParticipantsRequest(room=room.name))
    for participant in participants.participants:
        print(f"Participant SID: {participant.sid}")
I hope this helps you track active rooms and participants smoothly in real time. If you’d like, I’m here to assist with more examples or anything else you might need! Sources: livekit/livekit_room.twirp.go | examples/api.py
r
can you give me an example on how the output looks like for
list_rooms
method
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
r
what are the ways to get session_id?