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.
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.