CallEnded event doesn't trigger on call end but on ring end, alternatives?

Jeroen H 40 Reputation points
2024-05-02T13:36:14.97+00:00

I'm using the azure/communication-calling sdk in a react app from where users can receive teams calls. I'd like to subscribe to an event that notifies when a call has ended.

From the microsoft documentation I understood that in an incoming call event, you can also subscribe to a callEnded event. However, when testing this, it became clear that the event only triggers when an incoming call is accepted or declined (in other words, when the ringing stops), while always claims the callEndReason to be 'Cancelled'. Only the callEndReason's subcode changes depending on if the call was cancelled/declined/accepted.

  • The documentation on the callEnd event isn't very specific, but the name would be misleading if this situation was intended behaviour. Is it?
  • How else would I get an event for the call's end? I wish to at least know the call's duration, preferably using the azure/communication-calling library.
Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
704 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,904 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,351 Reputation points Microsoft Employee
    2024-05-03T04:22:45.1433333+00:00

    @Jeroen H The behavior you are experiencing is actually intended. The callEnded event is triggered when the call ends, but in the case of an incoming call, it is triggered when the ringing stops, which is when the call is either accepted or declined. The callEndReason subcode changes depending on whether the call was cancelled, declined, or accepted.

    To get an event for the call's end, you can subscribe to the callStateChanged event instead. This event is triggered whenever the call state changes, including when the call ends. You can check the state property of the Call object to see if the call has ended. The state property will be set to 'Disconnected' when the call ends.

    Here's an example of how you can subscribe to the callStateChanged event:

    const callStateChangedHandler = (args) => {
      const call = args.call;
      if (call.state === 'Disconnected') {
        const duration = call.duration;
        console.log(`Call ended after ${duration} seconds`);
      }
    };
    
    call.on('callStateChanged', callStateChangedHandler);
    
    

    In this example, we're checking if the call state is 'Disconnected' and logging the call duration when the call ends. You can use the duration property of the Call object to get the call duration in seconds.