hundreds-journalist-9108
09/07/2025, 11:07 AMtall-airline-16756
09/07/2025, 11:07 AMtall-airline-16756
09/07/2025, 11:08 AMRoomServiceClient.sendData() method to send data to everyone in the room. Here’s a quick example in Node.js:
import { RoomServiceClient } from 'livekit-server-sdk';
const roomService = new RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET);
await roomService.sendData(
roomName,
Buffer.from('Hello World'), // data as Uint8Array
DataPacket_Kind.RELIABLE // delivery kind
);
• Client SDK: If you’re a participant in the room, you can use LocalParticipant.publishData(). Here’s how it looks in Rust:
room.local_participant()
.publish_data(DataPacket {
payload: "Hello world".to_owned().into_bytes(),
reliable: true,
..Default::default()
})
.await
.unwrap();
You get to pick between reliable (which means in-order delivery) and lossy (best-effort delivery) options. Plus, if you want, you can add a topic or even target specific participants. Pretty flexible, right?
Sources: src/main/kotlin/io/livekit/server/RoomServiceClient.kt | cmd/lk/room.go | agent-console/app/api/room/send-data/route.ts | examples/basic_room_async/src/main.rs | Data packets | LiveKit Docshundreds-journalist-9108
09/07/2025, 11:11 AMtall-airline-16756
09/07/2025, 11:11 AMhundreds-journalist-9108
09/07/2025, 11:13 AMtall-airline-16756
09/07/2025, 11:14 AMAgentSession entrypoint, here’s a friendly step-by-step guide for you:
Sending a Custom Event
You can use the perform_rpc method to send a nicely structured payload to a specific participant like this:
import json
payload = {
"action": "show",
"id": "quiz_123",
"questions": ["What is LiveKit?", "How do agents work?"]
}
json_payload = json.dumps(payload)
await room.local_participant.perform_rpc(
destination_identity=participant.identity,
method="client.quiz",
payload=json_payload
)
This sends the event using the method name client.quiz directly to the participant you specify.
Handling the Event in the Entrypoint
To catch these incoming RPC calls, just register a handler with register_rpc_method like this:
async def entrypoint(ctx):
await ctx.connect()
async def handle_flip_flash_card(rpc_data):
payload = json.loads(rpc_data.payload)
card_id = payload.get("id")
if card_id:
print(f"Flipping card with ID: {card_id}")
return json.dumps({"status": "success", "id": card_id})
ctx.room.local_participant.register_rpc_method(
"agent.flipFlashCard",
handle_flip_flash_card
)
session = AgentSession()
await session.start(agent=agent, room=ctx.room)
This setup lets your agent respond to events sent from the client with the method name agent.flipFlashCard. Pretty neat, right?
Some Friendly Reminders
• Remember to use json.dumps() to turn your structured data into a string for perform_rpc.
• Your handlers will get an RpcInvocationData object containing the payload and who called it.
• Make sure your agent is connected to the room before sending or receiving any RPCs to keep things running smoothly.
If anything’s unclear or you want me to walk you through more examples, just give me a shout—I’m here to help!
Sources: metrics/send-metrics-to-3p/send_metrics_to_3p.py | Job lifecycle | LiveKit Docs | livekit-rtc/livekit/rtc/room.pyhundreds-journalist-9108
09/07/2025, 11:18 AMtall-airline-16756
09/07/2025, 11:19 AMhundreds-journalist-9108
09/07/2025, 11:21 AMtall-airline-16756
09/07/2025, 11:22 AM@ctx.room.on in LiveKit with Python, here’s a simple way to do it:
Sending a General Event
Just use the publish_data method on the room.local_participant object to send your data like this:
await room.local_participant.publish_data("hello world", topic="general_event")
Feel free to specify a topic to keep things organized, and you can pick between reliable or lossy delivery depending on what you need.
Handling the Event
To listen for the event, register a listener for data_received using @room.on("data_received") like this:
@room.on("data_received")
def on_data_received(data_packet):
print(f"Received data from {data_packet.participant.identity}: {data_packet.data.decode()}")
if data_packet.topic == "general_event":
print("This is a general event")
Complete Example
async def entrypoint(ctx):
room = ctx.room
@room.on("data_received")
def on_data_received(data_packet):
print(f"Message from {data_packet.participant.identity}: {data_packet.data.decode()}")
# Connect to the room
await ctx.connect()
# Send a general event
await room.local_participant.publish_data("hello world", topic="general_event")
And that’s it! This setup will help you send and handle room-wide events smoothly. If you have any questions or want to dive deeper, just let me know — I’m here to help!
Sources: livekit-rtc/livekit/rtc/room.py | Data packets | LiveKit Docshundreds-journalist-9108
09/07/2025, 11:23 AMawait room.local_participant.publish_data("hello world", topic="general_event") i need the room instance.
how can i get it in a backend server?tall-airline-16756
09/07/2025, 11:24 AMlist_rooms() method from the LiveKit API and filter by the room name you’re interested in. Here’s a simple example to get you started:
from livekit import api
import asyncio
async def get_room_instance(room_name: str):
# Set up the API client
lkapi = api.LiveKitAPI("https://your-livekit-server.com")
try:
# Fetch rooms filtered by the specific name
room_list = await lkapi.room.list_rooms(
api.ListRoomsRequest(names=[room_name])
)
if room_list.rooms:
room_instance = room_list.rooms[0]
print(f"Found room: {room_instance.name}")
print(f"Participants: {room_instance.num_participants}")
print(f"Created: {room_instance.creation_time}")
return room_instance
else:
print(f"Room '{room_name}' not found")
return None
finally:
await lkapi.aclose()
asyncio.run(get_room_instance("my-room"))
This way, you get all the room details you need! If anything’s unclear or you want to dive deeper, just give me a shout—I’m here to help!
Sources: livekit-protocol/livekit/protocol/room.pyihundreds-journalist-9108
09/07/2025, 1:06 PMtall-airline-16756
09/07/2025, 1:06 PMAgentSession using things like Voice Activity Detection (VAD) or Speech-to-Text (STT) endpointing. Just curious—are you working on this within an AI agent using the LiveKit Agents framework, or are you trying to detect turn end in a client app without agents? I’m here to help either way!
Sources: Pipeline nodes and hooks | LiveKit Docs | web/src/data/turn-end-types.ts | AI voice agents | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | livekit-rtc/livekit/rtc/_proto/room_pb2.pyhundreds-journalist-9108
09/07/2025, 1:09 PMtall-airline-16756
09/07/2025, 1:09 PM