ClearBlade / ClearBlade-Python-SDK

A Python SDK for interacting with the ClearBlade Platform.
Apache License 2.0
4 stars 4 forks source link

change publish message example #9

Closed jiang50 closed 5 years ago

jiang50 commented 5 years ago

In the code example of publishing MQTT messages, the platform receives all the messages at the same time after on_connect callback function return, because the background thread to publish messages is blocked in callback function.

When we call connect(), loop_start() function is also called to start a background thread that will process outgoing and incoming message buffer and trigger callback function. So, while callback function is executing, this background thread is blocked, as a result, it cannot send messages we published in the buffer.

The right way to write publish code is to put publish in the main thread, after calling connect(). We don’t need to wait for connect() because it is a blocking function and we already have default on-connect function in SDK to return error messages.