Class: ConnectionEvent

Defines connectionCreated and connectionDestroyed events dispatched by the Session object.

The Session object dispatches a connectionCreated event when a client (including your own) connects to a Session. It also dispatches a connectionCreated event for every client in the session when you first connect. (when your local client connects, the Session object also dispatches a sessionConnected event, defined by the SessionConnectEvent class.)

While you are connected to the session, the Session object dispatches a connectionDestroyed event when another client disconnects from the Session. (When you disconnect, the Session object also dispatches a sessionDisconnected event, defined by the SessionDisconnectEvent class.)

Example

The following code keeps a running total of the number of connections to a session by monitoring the connections property of the sessionConnect , connectionCreated and connectionDestroyed events:

var apiKey = ""; // Replace with your API key. See https://dashboard.nexmo.com
var sessionID = ""; // Replace with your own session ID.
                    // See https://developer.vonage.com/en/video/guides/create-session/.
var token = ""; // Replace with a generated token that has been assigned the moderator role.
                // See https://developer.vonage.com/en/video/guides/create-token/.
var connectionCount = 0;

var session = OT.initSession(apiKey, sessionID);
session.on("connectionCreated", function(event) {
   connectionCount++;
   displayConnectionCount();
});
session.on("connectionDestroyed", function(event) {
   connectionCount--;
   displayConnectionCount();
});
session.connect(token);

function displayConnectionCount() {
    document.getElementById("connectionCountField").value = connectionCount.toString();
}

This example assumes that there is an input text field in the HTML DOM with the id set to "connectionCountField" :

<input type="text" id="connectionCountField" value="0"></input>

Properties

Name Type Description
connection Connection A Connection object for the connection that was created or deleted.
connections Array Deprecated. Use the connection property. A connectionCreated or connectionDestroyed event is dispatched for each connection created and destroyed in the session.
reason String For a connectionDestroyed event, a description of why the connection ended. This property can have the following 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 the publisher 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) and was detected by the OpenTok platform.

Depending on the context, this description may allow the developer to refine the course of action they take in response to an event.

For a connectionCreated event, this string is undefined.

Methods

Name Description
isDefaultPrevented () → {Boolean} Whether the default event behavior has been prevented via a call to preventDefault() ( true ) or not ( false ).
preventDefault () Prevents the default behavior associated with the event from taking place.

isDefaultPrevented () → {Boolean}

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

preventDefault ()

Prevents the default behavior associated with the event from taking place.

To see whether an event has a default behavior, check the cancelable property of the event object.

Call the preventDefault() method in the callback function for the event.

The following events have default behaviors: