Search code examples
mqttmosquitto

Multiple published mosquitto, sub read only last published


I have a problem. With mosquitto, when I publish multiple data on the same topic, after published, I sub on this topic but display just the last value on this topic.

MacBook-Pro-de-Mathieu:~ mathieu$ mosquitto_pub -h localhost -t test -m "A" -r
MacBook-Pro-de-Mathieu:~ mathieu$ mosquitto_pub -h localhost -t test -m "B" -r

MacBook-Pro-de-Mathieu:~ mathieu$ mosquitto_sub -h localhost -t test
B

Only B is displayed when I sub to topic "test"

Is it normal? I want a queue with unlimited published, and display all my data when someone Sub to this topic.


Solution

  • This is working as designed and intended according to the MQTT spec. All MQTT brokers will behave this way.

    MQTT is a pub/sub protocol, not a message queuing system.

    You are using the -r flag with mosquitto_pub to set the retained flag on the messages when they are published.

    The broker only holds on to the most recent retained message for any given topic and delivers that to a new subscriber to that topic at the point of subscription. So when you publish the second message it overwrites the first.