Search code examples
azure-cognitive-servicesspeech

LeftConversation event for disconnected participants


I am using the quickstart template for multi-device conversations and it seems like the participant change event handler (participantsChanged) does not get fired when a participant disconnects. I would expect to get a LeftConversation for a participant that closes their browser window or loses internet connection, but it seems like the event is only fired when a participant chooses to disconnect.


Solution

  • The SpeechSDK.ParticipantChangedReason.LeftConversation event will be fired immediately if the participant leaves the conversation cleanly by clicking the 'Leave conversation' button.

    If the participant leaves the conversation by another means such as closing the browser window or clicking the browser's back button, a 'DisconnectSession' message will be immediately triggered in the underlying websocket. This will be elevated to a SpeechSDK.ParticipantChangedReason.LeftConversation event within 6 minutes. The websocket 'DisconnectSession' message is not currently exposed as a SDK event in the Javascript SDK.

    As a workaround, one possibility is to update the Quickstart code to add a listener for the browser 'beforeunload' or 'unload' event which will call the leave conversation function on the participant's behalf.

    https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event

    sample code:

    document.addEventListener("DOMContentLoaded", function () {
    // ... existing variable declarations
    
        window.addEventListener('beforeunload', (event) => {
            // Call LeaveConversation on the participant's behalf
            handleLeaveConversation();
            // Cancel the event as stated by the standard.
            event.preventDefault();
            // Chrome requires returnValue to be set.
            event.returnValue = '';
        });
    // existing code ...