How can we push a custom event to a room from exte...
# ask-ai
b
How can we push a custom event to a room from external API?
t
Thanks for reaching out, Gaurab. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to push a custom event to a room from an external API, you can do this easily using the RoomServiceClient from the LiveKit server SDKs. It lets you send any data you like to all or specific participants in a room through the
sendData
method. Here’s how you can do it: 1. First, authenticate with LiveKit using your API key and secret. 2. Then, use the
sendData
method to specify the room name, the data you want to send, the delivery kind (
RELIABLE
or
LOSSY
), and if you want, set a topic or target specific participants. Here’s a quick example in Node.js:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomService = new RoomServiceClient(
  process.env.LIVEKIT_URL,
  process.env.LIVEKIT_API_KEY,
  process.env.LIVEKIT_API_SECRET
);

await roomService.sendData(
  'my-room-name',
  new TextEncoder().encode('Hello participants!'),
  livekit.DataPacket_Kind.RELIABLE,
  {
    topic: 'custom_event',
    // destinationIdentities: ['user1', 'user2'] // optional: send to specific users
  }
);
On the client side, participants will get this data through the
dataReceived
event in the client SDKs. If you have any questions or want me to walk you through anything else, just let me know—I’m happy to help! Sources: agent-console/app/api/room/send-data/route.ts | mcp/server.py | src/main/kotlin/io/livekit/server/RoomServiceClient.kt | livekit-api/src/services/room.rs | Webhooks | LiveKit Docs
b
not client side, rather on Pyhthon agent side
t
Hey there! If you want to push a custom event to a room using Python, you can use the LiveKit API to send your data. Here's a simple example to get you started:
Copy code
from livekit.protocol import room as room_proto
from livekit.protocol.models import DataPacket
import json

async def send_data_to_room(room_name: str, message: str):
    chat_payload = {
        "message": message,
        "sender": "System",
        "timestamp": int(asyncio.get_event_loop().time() * 1000)
    }
    data = json.dumps(chat_payload).encode('utf-8')

    send_request = room_proto.SendDataRequest(
        room=room_name,
        data=data,
        kind=DataPacket.Kind.RELIABLE,
        topic="custom_event"
    )

    await lkapi.room.send_data(send_request)
Here’s a quick breakdown: • room_name: This is the name of the room where you want to send your event. • message: The actual custom event data you want to share. • kind: Use
DataPacket.Kind.RELIABLE
if you want to make sure the message gets delivered, or
LOSSY
if speed is more important. • topic: This is optional but handy for categorizing your message (like "custom_event"). Just a heads up—make sure you have the right API credentials and permissions to send data to the room. If you have any questions or want me to walk you through it, just let me know. I’m here to help!