in livekit agent, i'd like to register an event ha...
# ask-ai
m
in livekit agent, i'd like to register an event handler for participant connected event. how to do this?
t
Thanks for reaching out, Alfred. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To register an event handler for the participant connected event in a LiveKit agent, you can use the
on
method with the "participant_connected" event type. Here's an example implementation:
Copy code
@ctx.room.on("participant_connected")
def on_participant_connected(participant: rtc.RemoteParticipant):
    <http://logger.info|logger.info>(f"new participant joined {participant.identity}")
    # Add your custom logic here to handle the new participant
This registers a callback function that will be executed whenever a new participant connects to the room. The callback receives the
participant
object as an argument, which is an instance of
RemoteParticipant
containing information about the connected participant. Sources: Building an Automated IVR Menu Caller | LiveKit Docs | livekit.rtc API documentation
m
what if the handler is async function?
t
If the handler is an async function, you can register it in a similar way. Here's an example:
Copy code
@ctx.room.on("participant_connected")
async def on_participant_connected(participant: rtc.RemoteParticipant):
    <http://logger.info|logger.info>(f"new participant joined {participant.identity}")
    # Your async handler code here
This registers an async function as an event handler for the "participant_connected" event. When a new participant connects to the room, this function will be called with the
RemoteParticipant
object as an argument, allowing you to perform asynchronous operations within the handler. Sources: Building an Automated IVR Menu Caller | LiveKit Docs | livekit.rtc.room API documentation