how do i get the user from the room? python sdk ag...
# ask-ai
w
how do i get the user from the room? python sdk agent
t
Thanks for reaching out, Christian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get users (participants) from a room using the LiveKit Python SDK, you can follow these methods: • Getting All Remote Participants:
Copy code
# Iterate through all remote participants
for identity, participant in room.remote_participants.items():
    print(f"Identity: {identity}")
    print(f"Participant: {participant}")
    print(f"SID: {participant.sid}")
    print(f"Name: {participant.name}")
Or more simply:
Copy code
# Iterate through currently connected remote participants
for rp in room.remote_participants.values():
    print(rp.identity)
• Getting the Local Participant:
Copy code
local_participant = room.local_participant
print(f"Local participant identity: {local_participant.identity}")
• Getting a Specific Participant by Identity:
Copy code
# Get a specific remote participant by identity
participant = room.remote_participants.get("participant_identity")
if participant:
    print(f"Found participant: {participant.name}")
else:
    print("Participant not found")
Here is a complete example:
Copy code
import asyncio
from livekit import rtc

async def main():
    room = rtc.Room()
    
    # Connect to room
    await room.connect(URL, TOKEN)
    
    # Get local participant
    local_participant = room.local_participant
    print(f"Local participant: {local_participant.identity}")
    
    # Get all remote participants
    for identity, participant in room.remote_participants.items():
        print(f"identity: {identity}")
        print(f"participant: {participant}")
        print(f"participant sid: {participant.sid}")
        print(f"participant identity: {participant.identity}")
        print(f"participant name: {participant.name}")
        print(f"participant metadata: {participant.metadata}")
If you are working from a server context, you can also use the Room Service API to list participants:
Copy code
from livekit import api

async def list_room_participants(room_name: str):
    lkapi = api.LiveKitAPI()
    list_request = api.ListParticipantsRequest(room=room_name)
    participants_response = await lkapi.room.list_participants(list_request)
    
    for participant in participants_response.participants:
        print(f"Identity: {participant.identity}")
        print(f"Name: {participant.name}")
        print(f"SID: {participant.sid}")
Sources: livekit-protocol/livekit/protocol/room.py | Inside a session | LiveKit Docs