How can i publish a custom event from my agent (in...
# ask-ai
w
How can i publish a custom event from my agent (in python) so that my frontend can listen to that event using livekit react sdk
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you publish a custom event from your Python agent so your frontend can easily listen to it using the LiveKit React SDK. You can achieve this smoothly with Remote Procedure Calls (RPC). Here's a simple walkthrough: 1. Agent Sends Data via RPC: In your Python agent, you can use the
perform_rpc
method to send a payload directly to the frontend client. Here's an example:
Copy code
await ctx.room.local_participant.perform_rpc(
    destination_identity=client_participant.identity,
    method="receive_transcript",
    payload=json.dumps({
        "transcript": transcript.transcript,
        "timestamp": getattr(transcript, 'timestamp', None)
    })
)
This way, you're sending a custom message—like a transcript—to a specific frontend participant using the method name
receive_transcript
. It's pretty straightforward! 2. Frontend Registers RPC Handler: On the React frontend side, you’ll want to register a handler for this RPC method so it can listen for incoming messages. Here's how you can do it:
Copy code
room.localParticipant.registerRpcMethod("receive_transcript", (payload: string) => {
  const data = JSON.parse(payload);
  console.log("Received transcript:", data.transcript);
  // Update UI or state
});
This setup allows your frontend to respond in real time to the events your agent publishes, keeping everything in sync beautifully. If you'd like to dive deeper, these official starter repositories are fantastic resources to explore: Sources: basics/repeater.py | Web and mobile frontends | LiveKit Docs | Template System & Project Scaffolding | livekit/livekit_composite | DeepWik...
w
what is client_participant?
t
I'd love to help clarify what the term
client_participant
means! It refers to a remote participant in a LiveKit room, usually representing a frontend user like someone on a web or mobile client. In your application code, it's a variable name used to point to a specific
RemoteParticipant
object that your agent interacts with. When working with the Python agent, you can easily access remote participants through the
remote_participants
property of the room. Here's a simple example to illustrate:
Copy code
# Get remote participants
remote_participants = list(ctx.room.remote_participants.values())

# Assign the first remote participant as client_participant
client_participant = remote_participants[0]
The
RemoteParticipant
class is quite handy—it includes properties like
identity
,
sid
, and
metadata
. Plus, it offers methods to interact with the participant, such as sending RPC messages or accessing their published tracks. If you'd like, I’m here to provide more examples or explain anything further. Just let me know! Sources: complex-agents/note-taking-assistant/agent.py | livekit-rtc/livekit/rtc/room.py | Rooms, participants, and tracks | LiveKit Docs | Core Platform Architecture & Authentication | livekit/livekit_composite | D... | main.py
w
and from frontend side you use room object, from where to get it
t
In the LiveKit React SDK frontend, the
room
object is thoughtfully managed by the
LiveKitRoom
component and shared with its child components through React context. Here’s a friendly guide on how you can access it: 1. Using the
useLiveKitRoom
Hook:
The
LiveKitRoom
component uses the
useLiveKitRoom
hook behind the scenes to create and manage the
Room
instance for you. This hook sets up the room using the
serverUrl
,
token
, and other options you provide, then returns the
room
object so you can easily use it within your component tree. 2. Accessing Room via Context: After the
LiveKitRoom
component connects, it shares the
Room
object through
RoomContext.Provider
. You can grab it in any child component like this:
Copy code
import { RoomContext } from '@livekit/components-react';

function MyComponent() {
  const room = React.useContext(RoomContext);
  // Now you can use `room` to interact with participants, tracks, and more.
}
The
Room
object is packed with helpful properties like
localParticipant
(that’s you!) and
remoteParticipants
(everyone else in the room). If you’d like me to walk you through more examples or clarify anything, just let me know—I’m here to help! Sources: packages/react/src/components/LiveKitRoom.tsx | Connecting to LiveKit | LiveKit Docs