Have you explored the newest version of the OpenTok platform? Check out OpenTok 2.0, built on top of WebRTC. Learn more

SessionDisconnectEvent class

The Session object dispatches SessionDisconnectEvent object when a session has disconnected. This event may be dispatched asynchronously in response to a successful call to the disconnect() method of the session object.

SessionDisconnectEvent properties

The SessionDisconnectEvent object has the following properties:

cancelable (Boolean) — Whether the event has a default behavior that is cancelable (true) or not (false). You can cancel the default behavior by calling the preventDefault() method of the SessionDisconnectEvent object in the event listener function. The sessionDisconnect event is cancelable. (See preventDefault().)

reason (String) — A description of why the session disconnected. This property can have two values:

  • "clientDisconnected" — A client disconnected from the session by calling the disconnect() method of the Session object or by closing the browser. (See Session.disconnect().)
  • "forceDisconnected" — A moderator has disconnected you from the session by calling the forceDisconnect() method of the Session object. (See Session.forceDisconnect().)
  • "networkDisconnected" — The network connection terminated abruptly (for example, the client lost their internet connection).

target (Object) — The object that dispatched the event.

type (String) — The type of event. There is only one type of SessionDisconnectEvent object: "sessionDisconnected".

SessionDisconnectEvent methods

The SessionDisconnectEvent object has the following methods:

isDefaultPrevented():Boolean — Whether the default event behavior has been prevented via a call to preventDefault() (true) or not (false). See preventDefault().

preventDefault() — Prevents the default behavior associated with the event from taking place. For the sessionDisconnectEvent, the default behavior is: all Subscriber objects are unsubscribed, and Publisher objects are destroyed. If you call the preventDefault() method in the event listener for the sessionDisconnect event, the default behavior is prevented (and you can, optionally, clean up Subscriber and Publisher objects using your own code). For example, you may call preventDefault() and choose to leave the Publisher object while removing the Subscriber objects from the DOM (by calling the removeChild() method of the DOM object that contains the Subscribers). Call the preventDefault() method in the event listener function for the event.

Example

The following code initializes a session and sets up an event listener for when a session is disconnected.

var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
var sessionID = ""; // Replace with your own session ID.
                    // See https://dashboard.tokbox.com/projects/
var token = ""; // Replace with a generated token that has been assigned the moderator role.
                // See https://dashboard.tokbox.com/projects/

var session = TB.initSession(sessionID);
session.addEventListener("sessionDisconnected", sessionDisconnectedHandler);
session.connect(apiKey, token);

function sessionDisConnectedHandler(event) {
    alert("The session disconnected. " + event.reason);
}

IRC Live Chat

Have a quick question? Chat with TokBox Support on IRC. Join chat