Search code examples
hivemq

How does HiveMQ Persistence work?


What options does HiveMQ give for persistence features ? Is is file based by default? Can we change it to a database if we want ?

I tried to find something on net but all i could come up with was this MQTT-SQL-Database Plugin which uses MySQL db to store messages when they arrive. I want to persist everything like user subscriptions, stats data and any other data in to the database. Currently, if i restart the broker all data is lost.


Solution

  • Disclaimer: I'm working for the company behind HiveMQ

    HiveMQ 1.x and 2.x use a file based persistence by default (the files reside in the data folder). This works very well for most use cases. You can change the persistence to e.g. a database with the infinispan.xml file, this is not recommended, though. The data is really meant for HiveMQ internals and must not be modified externally.

    Also note, that statistics data is ephemeral and won't be persisted, so this won't survive a restart. User subscriptions survive restarts if the clients connected with a persistent session.

    I believe the best way to address your use case (persist all data like statistics, subscriptions, ...) is using the powerful HiveMQ plugin system. You can use the Services for getting data about subscriptions, clients, retained messages, ... anytime you want. Another approach would be to use the Callbacks provided by HiveMQ. You can intercept virtually every aspect in the broker (like when statistics are updated, someone subscribed, clients disconnected, ....).

    If you feel more comfortable digging into code directly, check out this example hello-world project on Github which uses quite a few callbacks and services. It's really easy to get started. If you want to see how to integrate database connection pools, check out this example project.

    The best way to ask HiveMQ related questions is to use the HiveMQ support forum, so check this out if you have more in-depth questions.