This question is about how to deal with inbound messages in the application.
For example, application is able to process only one message at a time, and is not ready to accept more messages (through callback) until some specific time. Or more specific case - what will happen if broker will try delivering more messages to the client that client can physically hold (e.g. free allocatable RAM restrictions)?
What is the best way for implementation in this case?
Right now I am performing mosquitto_loop()
manually. I see I can do mosquitto_loop_read()
when ready to receive message, and mosquitto_loop_write()
when ready to send message (e.g. after publish). But I am unsure because receiving messages, as well as publishing messages, involve both “read operations” and “write operations” (e.g. for publish send data and then receive PUBACK). What “read operations” means in mosquitto_loop_read()
- perform logical read from the topic (which involves sending and receiving packets), or perform physical receiving only?
Next, how do I control how many messages I receive using mosquitto_loop_read()
or mosquitto_loop()
? Is it something about max_packets argument? Why it is “currently unused” as there’s clear use case for it?
You shouldn't need to do anything special.
If you run the loop normally it will only handle one message at a time as long as you don't do any hand off to a separate thread in the message callback.
mosquitto_loop()
will call the message callback for each incoming message and block until it returns.
Now if you can't process messages on average as quickly as they arrive you have a problem, but if it's just short bursts the messages should back up in the broker (you need to look at the max in flight message settings and probably use better than QOS 0)