using js sdk, how to differentiate disconnection f...
# ask-ai
b
using js sdk, how to differentiate disconnection from the internal SDK method or triggered by "user" in this case manually called room.disconnect()?
t
Thanks for reaching out, Raphael. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you tell the difference between disconnections caused by the SDK’s internal methods and those you manually trigger using
room.disconnect()
in the JavaScript SDK, you can take a look at the
reason
property that comes with the
Disconnected
event. This property is part of the
DisconnectReason
enum and clearly tells you why the disconnection happened. So, when you or a participant manually calls
room.disconnect()
, the
Disconnected
event will fire with the reason
CLIENT_INITIATED
. On the other hand, if the disconnection is due to server or system actions (like the room being deleted or a participant being removed), you’ll see other reasons such as
ROOM_DELETED
,
PARTICIPANT_REMOVED
, or
DUPLICATE_IDENTITY
. Here’s a little example to guide you:
Copy code
room.on(RoomEvent.Disconnected, (reason) => {
  if (reason === DisconnectReason.CLIENT_INITIATED) {
    console.log("User manually disconnected.");
  } else {
    console.log("Disconnected by system or server:", reason);
  }
});
By checking the
reason
inside your
Disconnected
event handler, you’ll be able to easily tell manual disconnections apart from automatic ones. If you have any questions or need more help, feel free to ask—I’m here for you! Sources: packages/livekit-rtc/src/proto/participant_pb.ts | livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.... | Connecting to LiveKit | LiveKit Docs | src/e2ee/worker/ParticipantKeyHandler.ts