Understanding callbacks
When we declare a function, we pass this function as an argument to another function or method, or we assign this function to an attribute, and then some code calls this function at some time; this mechanism is known as callback. The name callback is used because the code calls back a function at some time.
The code declares three functions that we will specify as callbacks:
on_connect
: This function will be called when the MQTT client receives aCONNACK
response from the MQTT server, that is, when a connection has been successfully established with the MQTT server.on_subscribe
: This function will be called when the MQTT client receives aSUBACK
response from the MQTT server, that is, when a subscription has been successfully completed.on_message
: This function will be called when the MQTT client receives aPUBLISH
message from the MQTT server. Whenever the MQTT server publishes a message, based on the subscriptions for the client, this function will be called.
The...