When I call session.aclose, I don't receive a Disc...
# ask-ai
m
When I call session.aclose, I don't receive a Disconnected event from room.on inside of my client application. Is this a bug? Other event to listen for? Another way to handle? I want the user to be able to say "bye", a tool call happens, and then cleans up the room
t
Thanks for reaching out, Marcus. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you call
session.aclose()
, the LiveKit client should emit a
Disconnected
event on the
room.on
listener. This event indicates the participant has been disconnected and is the recommended event to listen for to handle cleanup. If you’re not seeing this event, some points to verify are: • Ensure you are correctly subscribing to the
Disconnected
event on the room object before calling
aclose()
. • Confirm the asynchronous
aclose()
call completes as expected. The
Disconnected
event is the standard trigger to perform your cleanup logic, such as releasing tracks or UI updates. Example usage:
Copy code
room.on(RoomEvent.Disconnected, (reason) => {
  // cleanup logic here
  console.log('Disconnected from the room:', reason);
});

await session.aclose();
This approach allows your user to say "bye", call
aclose()
on the session, and then reliably handle the disconnection with your subscribed event handler. Sources: Connecting to LiveKit | LiveKit Docs | src/room/events.ts | livekit-android-sdk/src/main/java/io/livekit/android/events/RoomEvent.kt
m
I'm calling aclose from my python server. My javascript web app is listening and never receives a disconnected event
t
Hey there! Let me help you understand what's happening with
session.aclose()
. It might seem like this would automatically disconnect your clients, but it actually doesn't work quite that way. When you call
aclose()
, it closes the server-side session, but your JavaScript clients won't automatically get a
Disconnected
event. I know, a bit tricky, right? No worries, though! To make sure your clients are properly notified, you'll want to use the server API to delete the room or shutdown the session. Something like
api_client.room.delete_room()
will do the trick and make sure all your connected clients get that
Disconnected
event. Here's a friendly example of how you might do this in Python:
Copy code
await api_client.room.delete_room(api.DeleteRoomRequest(
    room=ctx.job.room.name,
))

# Then optionally delete the room context
ctx.delete_room()
Bottom line: To wrap things up nicely and keep your clients in the loop, use the server API to delete the room instead of just calling
aclose()
. Hope this helps! Sources: livekit-agents/livekit/agents/worker.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Inside a session | LiveKit Docs | basics/exit_message.py
m
Copy code
declare enum DisconnectReason {
  /**
   * @generated from enum value: UNKNOWN_REASON = 0;
   */
  UNKNOWN_REASON = 0,

  /**
   * the client initiated the disconnect
   *
   * @generated from enum value: CLIENT_INITIATED = 1;
   */
  CLIENT_INITIATED = 1,

  /**
   * another participant with the same identity has joined the room
   *
   * @generated from enum value: DUPLICATE_IDENTITY = 2;
   */
  DUPLICATE_IDENTITY = 2,

  /**
   * the server instance is shutting down
   *
   * @generated from enum value: SERVER_SHUTDOWN = 3;
   */
  SERVER_SHUTDOWN = 3,

  /**
   * RoomService.RemoveParticipant was called
   *
   * @generated from enum value: PARTICIPANT_REMOVED = 4;
   */
  PARTICIPANT_REMOVED = 4,

  /**
   * RoomService.DeleteRoom was called
   *
   * @generated from enum value: ROOM_DELETED = 5;
   */
  ROOM_DELETED = 5,

  /**
   * the client is attempting to resume a session, but server is not aware of it
   *
   * @generated from enum value: STATE_MISMATCH = 6;
   */
  STATE_MISMATCH = 6,

  /**
   * client was unable to connect fully
   *
   * @generated from enum value: JOIN_FAILURE = 7;
   */
  JOIN_FAILURE = 7,

  /**
   * Cloud-only, the server requested Participant to migrate the connection elsewhere
   *
   * @generated from enum value: MIGRATION = 8;
   */
  MIGRATION = 8,

  /**
   * the signal websocket was closed unexpectedly
   *
   * @generated from enum value: SIGNAL_CLOSE = 9;
   */
  SIGNAL_CLOSE = 9,

  /**
   * the room was closed, due to all Standard and Ingress participants having left
   *
   * @generated from enum value: ROOM_CLOSED = 10;
   */
  ROOM_CLOSED = 10,

  /**
   * SIP callee did not respond in time
   *
   * @generated from enum value: USER_UNAVAILABLE = 11;
   */
  USER_UNAVAILABLE = 11,

  /**
   * SIP callee rejected the call (busy)
   *
   * @generated from enum value: USER_REJECTED = 12;
   */
  USER_REJECTED = 12,

  /**
   * SIP protocol failure or unexpected response
   *
   * @generated from enum value: SIP_TRUNK_FAILURE = 13;
   */
  SIP_TRUNK_FAILURE = 13,

  /**
   * server timed out a participant session
   *
   * @generated from enum value: CONNECTION_TIMEOUT = 14;
   */
  CONNECTION_TIMEOUT = 14,

  /**
   * media stream failure or media timeout
   *
   * @generated from enum value: MEDIA_FAILURE = 15;
   */
  MEDIA_FAILURE = 15,
}
If I delete the room will the egress still work? There has to be a more graceful way to handle
t
Hey there! Let me break down how LiveKit handles room and recording scenarios in a way that'll make total sense. When you delete a LiveKit room, any ongoing recordings or streams will automatically stop - it's like pulling the plug smoothly. This happens because the egress participant essentially leaves the room, which is a clean, built-in way to wrap things up. But I totally get that sometimes you might want more flexibility. If you'd prefer to keep the room running and have more control, you can use the
stopEgress
API. This gives you the power to stop recordings or streaming explicitly while keeping the room active for your participants. Best practices? Here's what I recommend: • Use
deleteRoom
when you want to cleanly shut everything down. • Or use
stopEgress
if you want to keep the room going and have more precise control. This approach lets you manage your recordings and rooms super flexibly, giving you the exact user experience you're looking for. Cool, right? Sources: packages/livekit-server-sdk/src/EgressClient.ts | pkg/service/redisstore_test.go | Inside a session | LiveKit Docs