marvinroger / async-mqtt-client

📶 An Arduino for ESP8266 asynchronous MQTT client implementation
MIT License
845 stars 267 forks source link

The messages are lost #295

Open sigmashig opened 1 year ago

sigmashig commented 1 year ago

I'm using AsyncMqttClient on ESP32 + Ardion-ESP. I found, that a lot of messages are lost. I would try to describe a problem:

  1. After establishing the connection, I subscribe to 25 topics.
  2. MQTT server is configured as 'Publish the last message on subscribe in 1000ms'. And I see in the server log, that message is really sent:
2023-04-30 08:24:18.155 - info: mqtt.0 (6565) Client [Board_7] subscribes on "mqtt.0.HeatPump.Board_7.Config.SSID"
2023-04-30 08:24:18.188 - info: mqtt.0 (6565) Client [Board_7] publishOnSubscribe

On the client, I provide a callback function for onMessage with just one line: put received topic+message to the Serial for testing. I didn't get a lot of messages, but when I look at server log I see:

2023-04-30 08:24:18.403 - debug: mqtt.0 (6565) Client [Board_7] send to this client "HeatPump/Board_7/Config/SSID": deleted As a result, I have got messages just for 4-5 topics from 25 subscriptions. What am I doing wrong? UPDATE: It happens when I run subscribe with QoS=2. I received all messages, when I subscribes with QoS=0. Why? I thought, that QoS=2 guarantees just that messages are delivered, but in real life it is not.

nanobrad commented 1 year ago

The QoS value used during subscription is the highest QoS you will accept. Meaning, if you subscribe with QoS of 0, then you will not be sent messages published with a QoS of 1 or 2. This is part of the protocol.

bertmelis commented 1 year ago

Two principles here:

When a message is published, it travels to the broker with a certain QoS. The broker then distributes it to the subscribers and possibly downgrading the QoS to match the clients subscription. QoS is, however, not upgraded. So a message sent with qos 2 can end at a client with qos 0. A message with qos 0 will be received and qos 0, also when the subscription is made with qos 2.

jbaudoux commented 7 months ago

I was facing similar issue and end-up on this thread. I'm subscribing to about 40 topics. Some topics where not receiving the messages. I analyzed the broker log and spotted that not all subscriptions had been registered. I solved the issue by calling yield() every few subscription. I think you need to have the subscription in loop to have it working. Calling delay(10) was also working.