opentok-web-samples icon indicating copy to clipboard operation
opentok-web-samples copied to clipboard

Invalid state transition: Event 'disconnect' not possible in state 'disconnected'

Open Noido opened this issue 5 years ago • 16 comments

Hi I try use session.disconnect() for disconnect clients and i got this error

Invalid state transition: Event 'disconnect' not possible in state 'disconnected'

My session is already connect so i dont understand

@msach22 ?

Thanks

Noido avatar Oct 14 '19 16:10 Noido

@Noido Hmm, are you listening for the sessionDisconnected event? Can you confirm that you're calling session.disconnect and have not received the event before?

msach22 avatar Oct 14 '19 17:10 msach22

yes sessionDisconnected

I have a function

function ready(fn) {
        if (document.readyState != 'loading'){
            fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }
 ready(function() {
  connectToSession();
    });

   function connectToSession() {
        session = OT.initSession(apiKey, sessionId);
        session.connect(token, function(err) {
            if (err) {
                console.error(err.message);
                return alert('An error occurred while connecting to the OpenTok session');
            }
            isSessionConnected = true;
        });
    }

And i add a btn for disconnect but when i click i got this msg

$('#disconnect').on('click',function(e){
        e.preventDefault();
        if(isSessionConnected == true){
            session.disconnect();
        }

    });

But got this error i dont understand why

Noido avatar Oct 14 '19 20:10 Noido

@Noido You're basing the disconnect method off of your flag. Please listen to the session disconnected event by doing the following to toggle your state:

session.on({
  sessionDisconnected: event => {
    isSessionConnected = false;
  },
});

msach22 avatar Oct 14 '19 23:10 msach22

I have the same issue; sessionDisconnected does not seem to do the job for my application. Application uses openTok @2.16.0.

Nasmar avatar Oct 18 '19 08:10 Nasmar

We have the same issue with OpenTok.js 2.16.3 08194dfcf You can see that we are calling session.disconnect() first, then we get the sessionDisconnected event, and then we get the invalid state transition. image

davideberlein avatar Dec 11 '19 11:12 davideberlein

It helped me doing all the following, when wanting to disconnect:

session.off();
session.disconnect();
session.unpublish(publisher, handleError)
publisher.destroy();
session.unsubscribe(subscriber);

But only run those when isSessionConnected to avoid further errors.

Nasmar avatar Dec 11 '19 12:12 Nasmar

@davideberlein Can you please share your flow for this error?

msach22 avatar Dec 16 '19 22:12 msach22

@msach22 this is the code ending a session:

function endSession() {
    // an observable that resolves synchronously and holds the Session object returned by `OT.initSession()`
    this.session$.take(1).subscribe(
        session => {
            session.off();
            session.disconnect();
        },
        e => $_tk.log_message("Error disconnecting session"));

    // an observable that resolves synchronously and holds the Publisher object returned by `OT.initPublisher()`
    this.publisher$.take(1).subscribe(
        publisher => publisher.destroy(),
        e => $_tk.log_message("Error destroying publisher"));
}

davideberlein avatar Dec 19 '19 08:12 davideberlein

Any updates on this issue?

duprez avatar Jul 30 '20 12:07 duprez

@duprez I'm still waiting for an answer from @msach22 or anyone else from opentok

davideberlein avatar Jul 30 '20 12:07 davideberlein

I am facing this issue still and even support is acting dumb at Vonage. Need a fix for this asap. Thanks @msach22

mmubasher avatar Oct 19 '20 11:10 mmubasher

Same here( Pls fix it @msach22 This didn't help :

closeCall = () => {
    this.session.off();
    this.session.disconnect();
    this.session.unpublish(this.publisher);
    this.publisher.destroy();
    this.session.unsubscribe(this.subscriber);
  };

SardukarSilver avatar Dec 09 '20 11:12 SardukarSilver

As a last resort, we had to switch to the browser based sdk. It seems the issues exists in NPM package only

mmubasher avatar Dec 14 '20 08:12 mmubasher

i get same issue, when using forceDisconnect(param)

danilrafiqi avatar Dec 28 '20 09:12 danilrafiqi

I got this working by manually unsubscribing each subscriber before disconnecting the session:

  const disconnectSession = useCallback(() => {
    if (tokboxSession) {
      tokboxStreams.forEach((stream) => {
        const subscribers = tokboxSession.getSubscribersForStream(stream);
        subscribers.forEach((subscriber) => tokboxSession.unsubscribe(subscriber));
      });
      tokboxSession.disconnect();
    }
  }, [tokboxStreams, tokboxSession]);

bryandowning avatar Jan 15 '21 00:01 bryandowning

I think it may be help!

if (session.currentState === 'connected') { session.off(); session.disconnect(); session.destroy(); session.unpublish(publisher); publisher.destroy(); session.unsubscribe('subscriber'); }

sunilmanipara avatar Apr 22 '21 09:04 sunilmanipara