Search code examples
mqttiotmosquitto

Synchronization Between publisher db and mosquitto broker db mqtt


My publisher and broker are working on different systems. The publisher has local DB in which it stores new outbound messages. I want to ask It is possible to have sync between publisher local DB with broker DB (mosquitto.db) when publisher lost connection with the broker and then broker automatically send these messages to the subscriber if subscriber connected to a broker or when connected to a broker.


Solution

  • The mosquitto.db store is purely for the the Mosquitto broker to keep internal state, this includes which clients have persistent sessions and any queued messages for those client. There is no access to it from anywhere else.

    If the publisher goes off line and then you try and publish a message it will throw an exception, it is up to you as the developer to catch the exception, store the message and try and resend it later when the publisher is reconnected to the broker.

    The paho client callback has a method (deliveryComplete()) that is called once a message has left the control of the client (this is effectively when it has reached the broker for QOS 1 or 2). When sending a message you should follow these steps:

    1. Store any new message in the publishers local database
    2. Try to send it to the broker
    3. Remove it (or mark it published) from the database when the deliveryComplete is called.

    On reconnection to the broker the database should be checked for any outstanding messages and they should be published.