Reputation: 103
I have a scenario wherein the MQTT client publishes a message with a topic . Post this I bring down my mosquitto broker (by killing the process) . When I restart my broker and try consuming the message with the topic , the message is not available . Is there any way I can consume the message which was published before the broker is restarted ?
Note : This is possible if RabbitMQ is used as the messages are stored in the queue . I want to implement the same using MQTT-Mosquitto.
Upvotes: 6
Views: 11397
Reputation: 249
You need to uncomment the following three options in the mosquitto.conf
file (dir/to/mosquitto/config/mosquitto.conf
):
persistence true
persistence_file mosquitto.db
persistence_location /path/to/store/data/
For me it was not enough to just enable the two options stated in the other answer to this question.
Regarding the persistence_location
please read the given comment in mosquitto.conf. However, you can just specify an absolute path, but you need to make sure, that the user who runs mosquitto (maybe root or not in your case) has the permission to write into this directory.
If you running mosquitto in a docker container based on the image eclipse-mosquitto:latest, your container should have mounted a docker volume, where you can point persistence_location
to.
Check with docker volume ls
if you have the volume mosquitto_data. In that case, you could set persistence_location mosquitto_data/
.
With docker inspect mosquitto_data
you get info about the docker volume, including the Mountpoint on the host system.
In this scenario you can also change your mosquitto.conf from the host system, it is in the docker volume mosquitto_conf
Upvotes: 0
Reputation: 59816
If you enable persistence and specify a directory in your mosquitto.conf file then retained messages will be saved to disk and restored when the broker is restarted.
e.g.
persistence true
persistence_location /path/to/store/data/
Also look at the autosave_interval
and autosave_on_changes
options in the docs
Upvotes: 10