This message was deleted.
# ask-ai
s
This message was deleted.
t
Thanks for reaching out, Bryce. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you need to remove someone from a LiveKit room, you can easily do that using the
RemoveParticipant
method from the RoomServiceClient in LiveKit's server SDKs. This will disconnect the participant and trigger a
Disconnected
event for them. Here’s a quick look at how you can do this with different SDKs: • Node.js:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomService = new RoomServiceClient(
  <LIVEKIT_SERVER_URL>,
  <API_KEY>,
  <API_SECRET>
);

await roomService.removeParticipant('room-name', 'participant-identity');
• Python:
Copy code
from livekit.api import RoomService, RoomParticipantIdentity

room_service = RoomService(
    url=<LIVEKIT_SERVER_URL>,
    api_key=<API_KEY>,
    api_secret=<API_SECRET>
)

await room_service.remove_participant(
    Remove: RoomParticipantIdentity(
        room='room-name',
        identity='participant-identity'
    )
)
• Go:
Copy code
package main

import (
    "context"
    "github.com/livekit/server-sdk-go"
    "github.com/livekit/protocol/livekit"
)

client := lksdk.NewRoomServiceClient(<LIVEKIT_SERVER_URL>, <API_KEY>, <API_SECRET>)
_, err := client.RemoveParticipant(context.Background(), &livekit.RoomParticipantIdentity{
    Room:     "room-name",
    Identity: "participant-identity",
})
You can also remove a participant using the LiveKit CLI like this:
Copy code
lk room participants remove PARTICIPANT_ID --room ROOM_NAME
Just a heads up: removing a participant doesn’t invalidate their access token, so if their token is still valid, they could join again. To keep them out, you might want to generate tokens with a shorter TTL or avoid issuing new tokens for that participant. Sources: Managing participants | LiveKit Docs | packages/livekit-server-sdk/src/RoomServiceClient.ts
c
can i remove a participant from inside an agent workflow?
t
Absolutely, you can remove a participant right from inside an agent workflow in LiveKit. This is done using the
RemoveParticipant
API, which you can call programmatically within your agent logic. For instance, in the
livepaint
agent, there's a method like
_kick_player
that uses
self._lkapi.room.remove_participant
to gently disconnect a participant by sending them a graceful disconnect RPC before removing them. The
RemoveParticipant
API is available in several server SDKs, including: • Kotlin SDK:
RoomServiceClient.removeParticipant(roomName, identity)
• Node.js SDK:
RoomServiceClient.removeParticipant(room, identity)
When you remove someone using this API, they’ll be disconnected from the room, and a
Disconnected
event will fire with the reason
PARTICIPANT_REMOVED
. Keep in mind, they can rejoin the room unless you add extra restrictions. Just a heads-up: this action needs
roomAdmin
permissions and is usually done from a trusted backend service or agent. Sources: src/main/kotlin/io/livekit/server/RoomServiceClient.kt | packages/livekit-server-sdk/src/RoomServiceClient.ts | agent/game_host.py | Connecting to LiveKit | LiveKit Docs | protobufs/livekit_room.proto
c
what is _lkapi?
t
Hey there! So, the term
_lkapi
isn’t directly explained in the docs. But usually, it’s a private or internal variable that points to an instance of the
LiveKitAPI
class from the LiveKit Python SDK. This class is your go-to for working with LiveKit services like room management, egress, ingress, SIP, and agent dispatch. For example, you’d typically see
lkapi
set up like this:
Copy code
from livekit import api
lkapi = api.LiveKitAPI("https://my-project.livekit.cloud")
If
_lkapi
is meant to be something else, just let me know! A bit more context would help clear things up. Sources: livekit-api/livekit/api/agent_dispatch_service.py
c
how can i list participants?
t
If you want to see who’s in a LiveKit room, you can use the
ListParticipants
API method from the RoomServiceClient. Just a heads-up, you’ll need
roomAdmin
permissions and the room name as input. This method will give you a list of
ParticipantInfo
objects, which have all the details about each participant in the room. Here’s a quick rundown on how you can do this with different SDKs: • Node.js SDK: Use
listParticipants(room: string)
, which returns a Promise that resolves to an array of
ParticipantInfo
. • Python SDK: Use
list_participants(list: ListParticipantsRequest)
, which returns a
ListParticipantsResponse
containing the participants. • Kotlin SDK: Use
listParticipants(roomName: String)
, which returns a Call object with the list of
ParticipantInfo
. • Go SDK: Use
ListParticipants(context.Background(), &livekit.ListParticipantsRequest{Room: roomName})
. • Rust SDK: Use
list_participants(&self, room: &str)
, which returns a Result with a vector of
ParticipantInfo
. • LiveKit CLI: Use the
listParticipants
command, which shows participant identity, state, and track count. The
ParticipantInfo
includes handy info like identity, session ID (SID), connection state, metadata, join timestamp, and published tracks. Sources: protobufs/livekit_room.proto | packages/livekit-server-sdk/src/RoomServiceClient.ts | livekit-api/livekit/api/room_service.py | src/main/kotlin/io/livekit/server/RoomServiceClient.kt | Managing participants | LiveKit Docs
c
how can i do this inside an agent context?
t
If you want to see who's in a LiveKit room while working within an agent context, you can use the
list_participants
method from the LiveKit server SDKs. For instance, in a Python agent, you’d use the
LiveKitAPI
client to call this method. Here’s a simple example to help you out:
Copy code
response = await self.job_context.api.room.list_participants(
    api.ListParticipantsRequest(room=room_name)
)
participants = response.participants
The response gives you details about each participant, including: • identity: The participant’s unique ID • name: The name they’re showing • state: Their current connection status • joined_at: When they joined the room • metadata: Any extra info tied to the participant • sid: Their session ID Just a heads-up: you’ll need
roomAdmin
permissions to use this, as it talks to the RoomService API. And if you’re using other SDKs like Go, Node.js, or the LiveKit CLI, you’ll find similar support there too. Sources: Managing participants | LiveKit Docs | packages/livekit-server-sdk/src/RoomServiceClient.ts | cmd/lk/room.go | livekit-api/livekit/api/room_service.py | telephony/sip_lifecycle.py
c
how do i import roomservice?
t
Hey there! To get started with importing and using RoomService in the LiveKit SDKs, it really depends on which programming language you're working with. No worries though—I’ve got some examples for a few popular SDKs to help you out: • Python:
Copy code
from livekit import api
room_service = api.RoomService()
• Node.js:
Copy code
const { RoomServiceClient } = require('livekit-server-sdk');
const roomService = new RoomServiceClient(LIVEKIT_SERVER_URL, API_KEY, API_SECRET);
• Go:
Copy code
import (
    "github.com/livekit/server-sdk-go"
)
roomService := lksdk.NewRoomServiceClient(LIVEKIT_SERVER_URL, API_KEY, API_SECRET)
Just swap out
LIVEKIT_SERVER_URL
,
API_KEY
, and
API_SECRET
with your actual LiveKit server URL and credentials. If you hit any bumps or want me to walk you through anything else, just give me a shout—I’m here to help!
c
self.ctx.room.remove_participant(participant.identity) can i do this? it says: ‘Room’ object has no attribute ‘remove_participant’
t
Hey there! Just a quick heads-up: the
Room
object itself doesn’t have a
remove_participant
method. Instead, you’ll want to use the
RoomService
API from the LiveKit server SDKs to remove a participant. It’s a bit different, but totally manageable with the
RoomService
client. Here’s a little example in Python to help you out:
Copy code
from livekit import api

room_service = api.RoomService()
await room_service.remove_participant(
    api.RoomParticipantIdentity(
        room="room-name",
        identity="participant-identity"
    )
)
Just remember to use the
RoomService
client instead of the
Room
object for this task. If anything’s unclear or you want to chat more about it, I’m here to help! Sources: livekit-api/src/services/room.rs | rpc/participant.pb.go | packages/livekit-server-sdk/src/RoomServiceClient.ts | livekit-api/livekit/api/room_service.py | lib/livekit/room_service_client.rb
c
room_service = api.RoomService() ^^^^^^^^^^^^^^^ AttributeError: module ‘livekit.api’ has no attribute ‘RoomService’. Did you mean: ‘room_service’?
t
Hey! The error you’re seeing happens because
RoomService
isn’t directly available as an attribute of
livekit.api
. Instead, you’ll want to create an instance of the
LiveKitAPI
class, which then gives you access to the
room
service to manage rooms and participants. Here’s a quick example of how to set up and use
RoomService
the right way:
Copy code
from livekit import api

# Initialize LiveKitAPI (make sure your environment variables for URL, API key, and secret are set)
lkapi = api.LiveKitAPI()
room_service = lkapi.room  # This is how you get the RoomService
After that, you can use
room_service
to do things like remove participants, list rooms, or update room details. For instance, to remove a participant:
Copy code
await room_service.remove_participant(
    api.RoomParticipantIdentity(
        room="room-name",
        identity="participant-identity"
    )
)
If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: examples/api.py | Managing rooms | LiveKit Docs
c
Traceback (most recent call last): File “/Users/bryceli/dev/livekit-realtime-demo/backend/agent.py”, line 66, in <module> lkapi = api.LiveKitAPI() ^^^^^^^^^^^^^^^^ File “/Users/bryceli/dev/livekit-realtime-demo/venv/lib/python3.12/site-packages/livekit/api/livekit_api.py”, line 59, in init self._session = aiohttp.ClientSession(timeout=timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/Users/bryceli/dev/livekit-realtime-demo/venv/lib/python3.12/site-packages/aiohttp/client.py”, line 316, in init loop = loop or asyncio.get_running_loop() ^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: no running event loop i get this then.
t
Hey there! That
RuntimeError: no running event loop
pops up because there isn’t an active asyncio event loop running at the moment. To fix this, you just need to make sure an event loop is up and running before you create your
LiveKitAPI
object. The easiest way to handle this is by using
asyncio.run()
, which takes care of the event loop for you. Here’s a quick example:
Copy code
from livekit import api
import asyncio

async def main():
    lkapi = api.LiveKitAPI("https://my-project.livekit.cloud")
    room_info = await lkapi.room.create_room(api.CreateRoomRequest(name="my-room"))
    print(room_info)
    await lkapi.aclose()

asyncio.run(main())
This way, the event loop is set up just right for
LiveKitAPI
to work smoothly. If you want me to walk you through it or have any other questions, just let me know—I’m here to help! Sources: examples/video-stream/video_play.py | stream_audio.py