inside livekit agent, how to see if the participan...
# ask-ai
b
inside livekit agent, how to see if the participant is in the room or not
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you check if a participant is in a room in LiveKit! You can do this using either server-side APIs or client-side SDKs, depending on what works best for you: Using Server-Side APIs: • List Participants: You can use the
ListParticipants
API to get all participants in a room. Just keep in mind, this needs the
roomList
grant. Then, you can easily see if a specific participant identity is in that list. • Get Participant Details: Another option is the
GetParticipant
API, which lets you fetch info about a particular participant. This requires the
roomAdmin
grant. If the participant isn’t currently in the room, the request will let you know by failing. Using Client-Side SDKs: • The
Room
object has a
remoteParticipants
array that includes all other participants in the room. You can go through this array to find the participant you’re looking for. • The
localParticipant
object represents you, the current user, in the room. Also, each participant has a
state
field that tells you their connection status, like
JOINING
,
JOINED
,
ACTIVE
, or
DISCONNECTED
. If you’d like, I’m here to help you with any part of implementing this—just let me know! Sources: Connecting to LiveKit | LiveKit docs | Rooms, participants, and tracks | LiveKit docs | livekit-rtc/livekit/rtc/room.py | Managing participants | LiveKit docs
b
@ctx.room.on("track_subscribed") def on_track_subscribed(track,publication,participant): can i grab the participant name from here who joined the meeting what are the other ways i can grab participant name or id etc
t
Absolutely! You can definitely grab the participant's name or other details from the
track_subscribed
event. Here’s a simple way to do it: Using the
track_subscribed
Event:
Copy code
@ctx.room.on("track_subscribed")
def on_track_subscribed(track, publication, participant):
    participant_id = participant.sid  # Server-generated unique ID
    participant_identity = participant.identity  # Developer-defined unique identifier
    participant_name = participant.name  # Optional display name

    print(f"Participant ID: {participant_id}")
    print(f"Participant Identity: {participant_identity}")
    print(f"Participant Name: {participant_name}")
Another Option: You can also tap into the
participant_connected
event to get participant details right when they join the room:
Copy code
@ctx.room.on("participant_connected")
def on_participant_connected(participant):
    print(f"Participant connected: {participant.identity} ({participant.name})")
    print(f"Participant SID: {participant.sid}")
If you have any questions or want me to walk you through this further, just let me know—I’m here to help! Sources: stream_audio.py | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | packages/livekit-rtc/src/proto/room_pb.ts | Rooms, participants, and tracks | LiveKit docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki