Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Event Hooks

Sean Perkins edited this page Sep 6, 2016 · 1 revision

Event hooks allow you to implement custom functionality around when a specific event takes place in OpenTok. Examples include, but are not limited to:

  • Display alternative to blank frame when unsubscribing
  • Custom app logic when publisher stops streaming

To better understand how to hook to existing events, you need to understand there are three different classes that may fire events.

  • TNSOTSession
  • TNSOTPublisher
  • TNSOTSubscriber

TNSOTSession

This class is responsible for all top-level events that are associated to the session. The available event names are:

  • sessionDidConnect
  • sessionDidDisconnect
  • sessionDidReconnect
  • sessionDidBeginReconnecting
  • streamCreated
  • streamDestroyed
  • didFailWithError
  • connectionDestroyed
  • connectionCreated
  • archiveStartedWithId
  • archiveStoppedWithId

To bind to these events, you will use the following example implementation:

import {TNSOTSession} from 'nativescript-opentok';

private session: TNSOTSession;

this.session = TNSOTSession.initWithApiKeySessionId('API_KEY', 'SESSION_ID');
this.session.events.on('EVENT_NAME', (result) => {
    // custom event hook logic here
});

For a complete list of the events and when they are executed in OpenTok's eco-system, please refer to their documentation here.

TNSOTPublisher

This class is responsible for all publisher related event call-backs. The available event names are:

  • streamCreated
  • streamDestroyed
  • didFailWithError

To bind to these events, you will use the following example implementation:

import {Page} from 'ui/page';
import {TNSOTSession, TNSOTPublisher} from 'nativescript-opentok';

private publisher: TNSOTPublisher;

constructor(private page: Page) {
    super();
    this.session = TNSOTSession.initWithApiKeySessionId(this._apiKey, this.sessionId);
    this.publisher = <TNSOTPublisher> this.page.getViewById('publisher');
    this.publisher.events.on('EVENT_NAME', (result) => {
        // custom event hook logic here
    });
}

For a complete list of the events and when they are executed in OpenTok's eco-system, please refer to their documentation here.

TNSOTSubscriber

This class is responsible for all subscriber related event call-backs. The available event names are:

  • didFailWithError
  • subscriberDidConnectToStream
  • didDisconnectFromStream
  • didReconnectToStream
  • subscriberVideoDisableWarning
  • subscriberVideoDisableWarningLifted
  • subscriberVideoDisabledReason
  • subscriberVideoEnabledReason

To bind to these events, you will use the following example implementation:

import {TNSOTSession, TNSOTSubscriber, TNSOTPublisher} from 'nativescript-opentok';

private subscriber: TNSOTSubscriber;

constructor(private page: Page) {
    super();
    this.session = TNSOTSession.initWithApiKeySessionId(this._apiKey, this.sessionId);
    this.subscriber = <TNSOTSubscriber> this.page.getViewById('subscriber');
    this.subscriber.events.on('EVENT_NAME', (result) => {
        // custom event hook logic here
    });
}

For a complete list of the events and when they are executed in OpenTok's eco-system, please refer to their documentation here.

Clone this wiki locally