android - Synchronous or Asynchronous messaging over MQTT is called first -


i have been reading this tutorial learn basics of paho android service api, , in text read client.connect(context, callback has 2 methods onsuccess() , onfailure()

as mentioned, synchronous client being used in example (mqttclient opposed mqttasyncclient). means requests, connect, block , return or throw exception. there no polling or read method messages server, messages server can arrive @ time. library provides callback mechanism handle behavior, methods in mqttcallback object registered client, invoked appropriate. mqttcallback interface must implemented class...to enable callback feature, callback object registered client, logically done prior connecting server

and after reading last 2 lines mentioned, got confused.because far understood, essence of having client registered calback hasconnectionlost,deliverycomplete,messagearrivedis handle server states "asynchronously" , read server.

now, question is, regarding last 2 lines quoted, how should register client read states server prior connecting server itself? or in other words, "why, "client.callback" should called prior "client.connect()"?"

can please clarify , explain point.

when set callback, you're doing registering function gets called when new message received. nothing happens until connect mqtt broker. should set callback before connect don't miss messages.

for example, if cleansession == 0, mqtt broker resume previous session when connect. if there messages waiting , haven't set callback function, might miss messages.

even if cleansession == 1, it's next command after connect mqttclient_subscribe. similar timing reasons, should set callback before call mqttclient_subscribe. it's either callback-connect-subscribe or connect-callback-subscribe when know cleansession == 1. not of difference, might habit of setting callback function before connect.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -