```how can i delete room on pariticpant disconnet`...
# ask-ai
e
Copy code
how can i delete room on pariticpant disconnet
t
Thanks for reaching out, Shehryar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To delete a room when a participant disconnects in LiveKit, you can use one of the following approaches: 1. Disconnecting the agent only: Use the
ctx.shutdown()
method to disconnect just the agent from the room, allowing other participants to continue in the session:
Copy code
async def entrypoint(ctx: JobContext):
    # do some work
    ...
    # disconnect from the room
    ctx.shutdown(reason="Session ended")
2. Disconnecting everyone and deleting the room: Use the server API's
deleteRoom
method to end the session for everyone and remove the room from the server:
Copy code
from livekit import api

async def entrypoint(ctx: JobContext):
    # do some work
    ...
    api_client = api.LiveKitAPI(
        os.getenv("LIVEKIT_URL"),
        os.getenv("LIVEKIT_API_KEY"),
        os.getenv("LIVEKIT_API_SECRET"),
    )
    await api_client.room.delete_room(api.DeleteRoomRequest(
        room=ctx.job.room.name,
    ))
This will send a
Disconnected
room event to all participants and remove the room from the server. You can detect when a participant disconnects using the appropriate event handler for your platform, such as the
room(_:participantDidDisconnect:)
delegate method in the Swift SDK. Sources: Inside a session | LiveKit Docs | room(_:participantDidDisconnect:) | Documentation
e
but i only want to delete room when outbound caller discounted or caller hungup the call.
t
To delete a room only when the outbound caller disconnects or hangs up the call, you can use the
delete_room
API to end the session for all participants. Here is an example implementation:
Copy code
# Add these imports at the top of your file
from livekit import api, rtc
from livekit.agents import get_job_context

# Add this function definition anywhere
async def hangup_call():
    ctx = get_job_context()
    if ctx is None:
        # Not running in a job context
        return

    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )

class MyAgent(Agent):
    # Your agent implementation

    @function_tool
    async def end_call(self, ctx: RunContext):
        """Called when the user wants to end the call"""
        # let the agent finish speaking
        current_speech = ctx.session.current_speech
        if current_speech:
            await current_speech.wait_for_playout()

        await hangup_call()
When the
delete_room
API is called, the
Disconnected
room event will be sent with a reason of
ROOM_DELETED
, and the room will be removed from the server, effectively ending the call for all participants. Sources: Agents telephony integration | LiveKit Docs | Inside a session | LiveKit Docs
e
Copy code
how can i get discount event if participant event
t
To detect when a participant disconnects in LiveKit, you can use the ParticipantDisconnected event, which is available across multiple SDKs: • Unity SDK:
public delegate void ParticipantDisconnectedDelegate(RemoteParticipant participant);
Swift SDK:
optional func room(_ room: Room, participantDidDisconnect participant: RemoteParticipant)
Android SDK:
class ParticipantDisconnected(val room: Room, val participant: RemoteParticipant) : RoomEvent
Flutter SDK:
ParticipantDisconnectedEvent({required RemoteParticipant participant})
This event is triggered when a
RemoteParticipant
leaves the room, allowing your application to respond appropriately to participant disconnections. Sources: Delegate Room.ParticipantDisconnectedDelegate | LiveKit Unity SDK | room(_:participantDidDisconnect:) | Documentation | ParticipantDisconnected | ParticipantDisconnectedEvent class - livekit_client library - Dart API
e
Copy code
can you provide python code
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.
g
Sin título
|hi @early-afternoon-97327 maybe you can try this
👍 1