Is it possible to add hooks to listen to events su...
# ask-ai
f
Is it possible to add hooks to listen to events such as participant has disconnected in the backend? Currently I am simply creating the access token for client to join, so livekit handles the creation of room. If i wish to add hooks to listen to events, does that mean i need to manually create the room myself and add the hook?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you understand how to listen for events like a participant disconnecting! There are two main ways you can do this: • SDK-based Listeners: You can use the
participant_disconnected
event available in LiveKit SDKs (like Python, JavaScript, Rust) if your backend is directly connected to the room. This is great for handling real-time actions such as bots or agents. • Webhooks: Another option is to use the
participant_left
webhook event, which notifies your backend via HTTP whenever a participant leaves. This works perfectly even if your backend isn’t connected to the room. If you’re letting LiveKit manage room creation for you, no worries—you can still use webhooks without needing to create rooms manually. But if you want to use SDK-based listeners, your backend would need to connect to the room directly. Please feel free to reach out if you'd like me to walk you through detailed steps for either method. I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | stream_audio.py | agent/game_host.py | Handling events | LiveKit Docs | examples/demo/demo.ts
f
what do u mean by backend is directly connected to the room?
t
When the backend is directly connected to the room, it means that your server application joins the LiveKit room as a participant (like a bot or agent). This setup lets your backend interact with the room in real-time, such as listening for events like
participant_disconnected
, sending messages, or managing participants. For instance, using the LiveKit SDK, your backend can join the room with an access token, just like any client would. This is especially helpful when your backend needs to actively participate in or keep an eye on the room's activity. If you'd like, I’d be happy to share more details or examples on how to get this set up—just let me know!
f
what if i am using the livekit agentsession? is that able to listen for events like participant_disconnected?
t
Absolutely! LiveKit's
AgentSession
does handle participant disconnection events, though it does this through its connection with the room. Let me walk you through how it works: • The
AgentSession
uses a
Room
object behind the scenes, which emits a
participant_disconnected
event whenever someone leaves. • If you have the
close_on_disconnect
option enabled (which is the default), the
AgentSession
will automatically close when the participant it’s interacting with disconnects. You can listen for the
close
event on the
AgentSession
to catch this. • Or, if you want more control, you can listen directly to the
participant_disconnected
event on the
Room
object. If you’d like, I’m happy to share some examples or dive deeper into how to set this up. Just let me know! Sources: examples/other/transcription/multi-user-transcriber.py | agents/src/voice/events.ts | agent/game_host.py