Detecting Receiver Disconnection in OpenTok Sessions: A Workaround Using Timers

Understanding Opentok’s Session Management for End Call Events

When building an iPhone app using OpenTok for video conferencing and real-time communication, managing the end of a call is crucial. In this article, we’ll delve into the specifics of Opentok’s session management and explore how to detect when a receiver ends a call without accepting it.

Introduction to Opentok Sessions

OpenTok provides an SDK that enables developers to create high-quality video conferencing and real-time communication applications. At its core, OpenTok uses WebSockets and signaling protocols to establish connections between clients. These connections are represented by OTSession objects, which manage the flow of data between participants.

When a participant initiates or accepts a call, they form an OTSession connection with the receiver. This connection allows them to exchange media streams (audio and video) in real-time. The OTSession object is responsible for managing this connection and handling events such as connection establishment (connectionCreated) and disconnection (connectionDestroyed).

Connection Creation and Destruction Events

To understand how OpenTok detects end call events, it’s essential to grasp the role of connection creation and destruction events. When a participant initiates a call, their client establishes an OTSession connection with the receiver. If the receiver accepts the call, the connection is formed successfully. Conversely, if the receiver rejects or ignores the invitation, the connection remains inactive.

When the receiver ends the call (either by accepting or rejecting it), their client sends a disconnect request to OpenTok’s servers. This request triggers the connectionDestroyed event on the receiver’s OTSession object. Similarly, when a participant initiates a call and the receiver accepts it, the receiver’s OTSession connection is formed successfully, triggering the connectionCreated event.

The Challenge: Receiver Doesn’t Accept Calls

The problem arises when the receiver never picks up the call and therefore never connects to the OpenTok Session. In this scenario, the caller’s client will not receive any notifications about the receiver’s connection creation or destruction.

To overcome this challenge, we’ll explore a workaround using timers and custom logic on both the caller and receiver sides.

Workaround: Using Timers to Detect Receiver Disconnection

One approach is for the caller’s client to start a timer when initiating the call. If the connectionCreated event does not occur within a specified time frame (e.g., 1 minute), it can infer that the receiver has either rejected or ignored the invitation.

Here’s an example implementation:

- (void)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:60 // 1-minute interval
                                                    target:self
                                                selector:@selector(checkConnectionState)
                                                        userInfo:nil
                                                         repeats:YES];
}

- (void)checkConnectionState {
    if (!self.session.connected) {
        // Receiver has disconnected or rejected the call
        self.doDisconnect();
    }
}

In this example, we start a timer when initiating the call. When the timer expires, it checks the OTSession connection state and calls the doDisconnect method if the receiver is no longer connected.

Receiver Side Implementation

On the receiver’s side, you can use similar logic to detect when a caller initiates a call but does not receive an acceptance response. You can start a timer and check for the presence of the OTSession connection within the specified time frame.

Here’s an example implementation:

- (void)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:60 // 1-minute interval
                                                    target:self
                                                selector:@selector(checkConnectionState)
                                                        userInfo:nil
                                                         repeats:YES];
}

- (void)checkConnectionState {
    if (!self.session.connected && !self.session.pending) {
        // Caller has initiated a call but did not receive an acceptance response
        self.doDisconnect();
    }
}

In this example, we start a timer when initiating the call. When the timer expires, it checks the OTSession connection state and calls the doDisconnect method if the caller is still waiting for an acceptance response.

Conclusion

Managing end call events in OpenTok sessions can be challenging when dealing with scenarios where receivers reject or ignore invitations. By using timers and custom logic on both the caller and receiver sides, you can detect when a receiver ends a call without accepting it.

Remember to adapt this approach to your specific use case and adjust the timer intervals as needed.


Last modified on 2023-06-06