F. Mbea
F. Mbea

Reputation: 11

How do I disconnect clients from the session with opentok?

Anytime I call the session.disconnect() method to remove clients from the session, I get this warning: "OpenTok:Publisher:warn Received connectivity event: "Cancel" without "Attempt"

and this error: "OpenTok:Subscriber:error Invalid state transition: Event 'disconnect' not possible in state 'disconnected'"

Could someone please explain to me what that error means? Thanks in advance.


// Initialize the session
      var session = OT.initSession(data['apikey'], data['session_id']);
      console.log(session);

      // Initialize the publisher for the recipient
      var publisherProperties = {insertMode: "append", width: '100%', height: '100%'};
      var publisher = OT.initPublisher('publisher', publisherProperties, function (error) {
        if (error) {
          console.log(`Couldn't initialize the publisher: ${error}`);
        } else {
          console.log("Receiver publisher initialized.");
        }
      });
      $('#session-modal').modal("show");

      // Detect when new streams are created and subscribe to them.
      session.on("streamCreated", function (event) {
        console.log("New stream in the session");
        var subscriberProperties = {insertMode: 'append', width: '100%', height: '100%'};
        var subscriber = session.subscribe(event.stream, 'subscriber', subscriberProperties, function(error) {
          if (error) {
            console.log(`Couldn't subscribe to the stream: ${error}`);
          } else {
            console.log("Receiver subscribed to the sender's stream");
          }
        });
      });

      //When a stream you publish leaves a session, the Publisher object dispatches a streamDestroyed event:
      publisher.on("streamDestroyed", function (event) {
        console.log("The publisher stopped streaming. Reason: "
        + event.reason);

      });

      //When a stream, other than your own, leaves a session, the Session object dispatches a streamDestroyed event:
      session.on("streamDestroyed", function (event) {
        console.log("Stream stopped. Reason: " + event.reason);
        session.disconnect();
        console.log("called session.disconnect().");


      });


      session.on({
        connectionCreated: function (event) {
          connectionCount++;
          if (event.connection.connectionId != session.connection.connectionId) {
            console.log(`Another client connected. ${connectionCount} total.`);
          }
        },
        connectionDestroyed: function connectionDestroyedHandler(event) {
          connectionCount--;
          console.log(`A client disconnected. ${connectionCount} total.`);
        }
      });

      // Connect to the session
      // If the connection is successful, publish an audio-video stream.
      session.connect(data['token'], function(error) {
        if (error) {
          console.log("Error connecting to the session:", error.name, error.message);
        } else {
          console.log("Connected to the session.");
          session.publish(publisher, function(error) {
            if (error) {
              console.log(`couldn't publish to the session: ${error}`);
            } else {
              console.log("The receiver is publishing a stream");
            }
          });
        }
      });

      // Stop the publisher from streaming to the session if the user dismiss the modal
      const stopSession = document.getElementById('stop-session');
      stopSession.addEventListener("click", (event) => {
        event.preventDefault();
        session.disconnect();
      });


Upvotes: 1

Views: 2070

Answers (2)

TCSoft
TCSoft

Reputation: 31

I see this is kind of old, but wanted to share my solution to avoid this error. I'm not sure what the error means, but I call publisher.destroy() before calling session.disconnect() to avoid the error.

openTokPublisher.destroy();
openTokSession.disconnect();

Upvotes: 4

Kishan Patel
Kishan Patel

Reputation: 181

I highly doubt you can disconnect the client with JavaScript. Here what I did.

// Connect to the session
session.connect(token, function connectCallback(error) {
 // Get the connectionId
    connectionId = session.connection.connectionId;

and use one of their SDK on the backend https://tokbox.com/developer/sdks/server/

// Disconnect session
function disconnectSession() { // eslint-disable-line no-unused-vars
 if (sessionId && connectionId) {
    $.ajax({
        url: '/OpenTok/DisconnectSession',
        type: 'POST',
        data: 'sessionId=' + sessionId + '&connectionId=' + connectionId,
    });
   }
}

Upvotes: 0

Related Questions