websocketxmppmultiuserchatstrophe.js

How to make sure that a chat-room was left successfully? (instance.connection.muc.leave())


Here I've found that there's an ability to add a callback as the 3rd argument to a leave method: https://github.com/metajack/strophejs-plugins/blob/96da306f5394b901e190a3f7365fbbb676fddb51/muc/strophe.muc.js#L117

But it doesn't work. How to add a callback or something else to a instance.connection.muc.leave() method to make sure that a chat-room was left successfully? Maybe there are some more ways to make sure that we have left a chat-room?

function handler_cb() {
  console.log('>>>>>>>>> leave was successfull');
}
function leaveChat() {
  instance.connection.muc.leave(room, nick, handler_cb);
}

Solution

  • I think there is a mistake in the plugin itself

    They use presenceid (stanza id) attribute to match a response from server https://github.com/metajack/strophejs-plugins/blob/master/muc/strophe.muc.js#L146

    but not all the servers use stanza id in presences. Actually there is nothing about presence ID in XEP-0045 exit presence, so some servers may implement an ID echo logic but mostly not (and looks like your XMPP server as well)

    So I recommend to set this header by yourself, w/o 'presenceid':

    function leaveChat() {
      instance.connection.addHandler(leaveCallback, null, "presence", "unavailable");
      instance.connection.muc.leave(room, nick);
    }
    
    function leaveCallback() {
      console.log('>>>>>>>>> leave was successfull');
      instance.connection.deleteHandler(leaveCallback);
    }