how to detect DUPLICATE_IDENTITY in disconnect eve...
# ask-ai
a
how to detect DUPLICATE_IDENTITY in disconnect event when using js
t
Thanks for reaching out, 文凱. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking into detecting the
DUPLICATE_IDENTITY
disconnect reason in your JavaScript application using LiveKit. To do this, you can listen to the
disconnected
event on the
Room
object. This event gives you a
reason
parameter of type
DisconnectReason
, which tells you why the participant was disconnected. You can check if it matches
DisconnectReason.DUPLICATE_IDENTITY
to handle that specific case. Here’s a simple example to guide you:
Copy code
import { DisconnectReason } from 'livekit-client';

room.on('disconnected', (reason) => {
  if (reason === DisconnectReason.DUPLICATE_IDENTITY) {
    console.log('Disconnected due to duplicate identity');
    // You might want to show a friendly message to the user here
  } else {
    console.log('Disconnected for another reason:', reason);
  }
});
This way, you can gracefully handle situations where someone else joins the room with the same identity, causing the previous participant to be disconnected. If you need any more help or have questions, feel free to ask—I'm here to support you! Sources: Connecting to LiveKit | LiveKit Docs | agent-console/lib/event-types.ts | packages/livekit-rtc/src/proto/participant_pb.ts | lib/src/proto/livekit_models.pbenum.dart | Runtime/Scripts/Room.cs