How to log reason for @ctx.room.on("disconnected")...
# ask-ai
b
How to log reason for @ctx.room.on("disconnected") event
t
Thanks for reaching out, Håvard. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To log the reason for the
@ctx.room.on("disconnected")
event in LiveKit, you can utilize the
Disconnected
event's reason property. This property provides details about why the disconnection occurred, such as: • DUPLICATE_IDENTITY: Another participant with the same identity joined the room. • ROOM_DELETED: The room was closed via the
DeleteRoom
API. • PARTICIPANT_REMOVED: The participant was removed using the
RemoveParticipant
API. • JOIN_FAILURE: Connection to the room failed, possibly due to network issues. • ROOM_CLOSED: The room was closed because all participants left. By accessing the
reason
property in the event handler, you can log this information for debugging or monitoring purposes. Sources: Connecting to LiveKit | LiveKit Docs | Disconnected
b
Can you show me a code example using Python
t
Here is a Python code example for logging the reason in the
@ctx.room.on("disconnected")
event in LiveKit:
Copy code
import logging

async def entrypoint(ctx):
    # Connect to the room
    await ctx.connect()

    @ctx.room.on("disconnected")
    def on_room_disconnected(event):
        <http://logging.info|logging.info>("Room disconnected, reason: %s", event.reason)
        # Log if there was an error
        if event.error:
            logging.error("Disconnect error: %s", event.error)

    # Rest of your code...
This example sets up an event handler for the
"disconnected"
event on the room object, which logs the reason for disconnection and any associated error. Sources: Tree View: | dse-workbench
b
This leads to this error: ""AttributeError: 'int' object has no attribute 'reason'. ""
t
I'm sorry, but I don't have the information you need at the moment. If you would like a member of the LiveKit team to respond, please ask your question in one of the other channels.
b
What is the type of the event emitted, and how can I import it?