Manage Sessions while using OpenTok

In this blog post I am going to share the experience of integrating my Android app with OpenTok.

Basically OpenTok SDK is WebRTC protocol based audio/video calling platform which is currently available for iOS and Android platform.

What is WebRTC :-

WebRTC is a real-time communication protocol which provides support for sharing audio, video and data, using a simple api. More details about WebRTC can be found here.

Starting With OpenTok:-

As I already mentioned currently OpenTok is available for Android and iOS. They have a simple SDK for both platforms, using these SDKs we can easily configure the OpenTok libs in our project.

To start with OpenTok we require a Client Key which can be obtained by registering on the OpenTok website. As it’s a paid SDK, so with demo account you have a limitation for sessions and number of audio/video call participants.

That is the basic introduction about OpenTok and you can find more information and tutorials on OpenTok developer site.

But my motive is here to tell something which can only be discovered by experience, which I received by integrating and using OpenTok.

Hopefully I will be able to save you some time.

Let’s first talk about the requirement

My application is basically a chat based application where you have multiple conversations (Groups) and along with chat you can start audio/video call within conversation. So for our business requirements for audio/video call we were totally dependent on OpenTok.

And for audio/video call in OpenTok our server has created a Session Id for every conversation. So when any user initiates an audio/video call we need to notify the target user too.

For achieving that we have to connect with every available session in app, on app startup.

For example If I have 20 conversations in my app, so that means I am already having 20 session ids for those (Every Id is associated with a single conversation) which I already got from server.

So on app start-up we connected with all sessions and start listening for subscriber stream (Subscribers are the users other than me in the  same conversation). In this way if any subscriber initiates an audio/video call we will get callback for that and prompt user about the incoming call.

But unfortunately this approach didn’t work. Although OpenTok SDK documentation says we can connect with multiple sessions at the same time, but when we tried to connect with all the sessions on app startup, most of the times, we got crashes in the OpenTok SDK.

We discussed this with OpenTok Support as well, but finally they advised us to connect with single sessions, and on reverting to single sessions we were able to get rid of the crashes.

So while using OpenTok I want you to keep some small things in mind:

  1. Connect with single session at one time, and before connecting properly release the resources and disconnect with any older session.
  2. You can maintain the session’s Pause and Resume state, as per application’s lifecycle, for e.g. when application is going in the background then session can be paused, and when it comes back to foreground, the session can be resumed. This is the recommended behaviour, even though it is not mandatory.
  3. Every session can have two type of streams Publisher or Subscriber.
    1. When you subscribe a subscriber then only subscribe it once not more than once if you try to do that then it will disconnect the session internally and audio/video call behave unusual.
    2. Publisher stream type can be of two type :- CAMERA or SCREEN_SHARE.
    3. To change in stream type for publisher you need to create a new publisher although publisher has the option to change Stream type but it does not work.
    4. Every time when you hang up a call properly unsubscribe all the subscriber’s stream and un-publish the publisher. As It using lots of device resources so properly releasing resources after your task is done will save lots of battery and memory usage.

These points should be helpful in implementing OpenTok. We had made these minor mistakes which took some time to resolve.

Hope you found this post useful!

You might also like