QuickBlox / quickblox-javascript-sdk

JavaScript SDK of QuickBlox cloud backend platform
Other
105 stars 217 forks source link

Logging out and Destroying Session #191

Closed ganeshub closed 5 years ago

ganeshub commented 7 years ago
var user = {
      id: 4448514,
      name: 'chatuserweb1',
      login: 'chatuserweb1',
      pass: 'chatuserweb1'
    };

QB.createSession({login: user.login, password: user.pass}, function(err, res) { //async call
  if (res) {
    QB.chat.connect({userId: user.id, password: user.pass}, function(err, roster) { // async call
      if (err) {
          console.log(err);
      } else {

           msg = {
                'type': type,
                'body': attach.type === 'location' ? 'Location' : 'Attachment',
                'extension': {
                    'save_to_history': 1,
                    'dialog_id': dialog_id,
                    'date_sent': time,
                    'attachments': [
                        attach
                    ]
                },
                'markable': 1
            };

            msg.id = QB.chat.send(jid, msg); // sync call

      }
    });
  }else{
    console.log(err);
  }
});

How/Where to call QB.chat.disconnect() (sync call) and QB.destroySession (async call) so the node process will exit properly. (when using in worker server process)

Vladlukhanin commented 7 years ago

You can do these actions anytime and anywhere.

Logout from chat: https://quickblox.com/developers/Web_XMPP_Chat_Sample#Logout_from_Chat

Destroy a session:

QB.destroySession(function(error,result) {
    if (result) {
        console.log(result); // Status: 200, null
        /* do something if you need after get response */
    } else {
        console.log(error);
        /* do something if you need after get error */
    }
});

Or what do you mean?

ganeshub commented 7 years ago

Thanks @Vladlukhanin

I am confused about Send call, It is a sync call but I've to wait for it to finish until I call disconnect and destroySession right ?

Note: I am using Node.js (worker process) not browser

Vladlukhanin commented 7 years ago

We need create a session for call REST API methods. It's almost all QB methods where we have callbacks functions, send some request and get a response or an error. And we need connect to chat for comunicate with chat (live). Chat's methods can send something and recieve by listeners functions. For example QB.chat.send(userId, msg) - QB.chat.onMessageListener(userId, msg). In our SDK we using callback for QB.chat.connect(params,callback) (*we call callback function when chat will be connected). You can do QB.destroySession(callback) and QB.chat.disconnect(), when/where you need. Try to do in your code. Guide. Getting Started with Chat API: https://quickblox.com/developers/Web_XMPP_Chat_Sample#Guide:_Getting_Started_with_Chat_API

ganeshub commented 7 years ago
var user = {
      id: 4448514,
      name: 'chatuserweb1',
      login: 'chatuserweb1',
      pass: 'chatuserweb1'
    };

QB.createSession({login: user.login, password: user.pass}, function(err, res) { //async call
  if (res) {
    QB.chat.connect({userId: user.id, password: user.pass}, function(err, roster) { // async call
      if (err) {
          console.log(err);
      } else {

           msg = {
                'type': type,
                'body': attach.type === 'location' ? 'Location' : 'Attachment',
                'extension': {
                    'save_to_history': 1,
                    'dialog_id': dialog_id,
                    'date_sent': time,
                    'attachments': [
                        attach
                    ]
                },
                'markable': 1
            };

            msg.id = QB.chat.send(jid, msg); // sync call
            QB.chat.disconnect() // Is it ok to call disconnect here

      }
    });
  }else{
    console.log(err);
  }
});

This is a Node.js worker process so there is no separate logout event (like in browser) to call disconnect(). So I have to call disconnect from the connect's callback call itself.

For example think of code like which do these things in order in a worker process

-> Worker Task Queued 
-> Worker Task Started 

1. createSession
2. connect to chat
3. send a message
4. disconnect from chat
5. destroySession (Node.js process should exit properly after this call)

-> Worker Task is done

For example, This code will properly exit the running Node.js process after 2 Seconds

console.log('wait 2 seconds');

function cb() {
  console.log('timed out.'); // Node.js process will exit after printing this to console
};

setTimeout(cb, 2000);

How do I achieve the same with above QB Node.js api calls ?

vadimkhmyrovqb commented 5 years ago

Closed due to inactivity.