Search code examples
opentok

Opentok | Get online users


This is my code for opentok connect. How to know when other user connected or disconnected to the session ? So i can populate my online friends list like social network site. Then i can initiate a chat with them.

function connect() {
        OT.on("exception", exceptionHandler);

        // Un-comment the following to set automatic logging:
        OT.setLogLevel(OT.DEBUG);

        if (!(OT.checkSystemRequirements())) {
            alert("You don't have the minimum requirements to run this application.");
        } else {
            session = OT.initSession(sessionId);    // Initialize session
            session.connect(apiKey, token);

            // Add event listeners to the session

            session.on('sessionConnected', sessionConnectedHandler);
            session.on('sessionDisconnected', sessionDisconnectedHandler);
            session.on('connectionCreated', connectionCreatedHandler);
            session.on('connectionDestroyed', connectionDestroyedHandler);
            session.on('streamCreated', streamCreatedHandler);
            session.on('streamDestroyed', streamDestroyedHandler);
            session.on("signal", signalEventHandler);

        }

    }

function sessionConnectedHandler(event) {
    // i am connected .... do something after connected
    document.getElementById("User_name").innerHTML = user_name;
    document.getElementById("disconnectLink").style.display  = 'block';


    //startPublishing();

}

Solution

  • The function connectionCreatedHandler will get called each time another user connects, and connectionDestroyedHandler will get called each time a user disconnects.

    For a sample app that has a "Buddy List" like functionality, see Presence Kit. Demo: http://presencekit-php.herokuapp.com/