Durable session store
Hi I would like to ask on recommendations regarding durable storage for sessions. I would like all the topics and messages inside of those topics to be durably stored, ideally in similar fashion as it is possible with retained messages and WithStorage option. Please any suggestions how to achieve that?
Which project is your question related to?
- Server
Well, you could intercept all messages and connections using the handlers described in the wiki:
- https://github.com/chkr1011/MQTTnet/wiki/Server#intercepting-application-messages
- https://github.com/chkr1011/MQTTnet/wiki/Server#intercepting-subscriptions
- https://github.com/chkr1011/MQTTnet/wiki/Server#validating-mqtt-clients
and store them in a database or file(s)?
Thank you @SeppPenner this was my original thought, nevertheless as this is not totally trivial task, when we consider cleaning of the db/store, eventually parallel access etc. I wanted to know, if there is some out of the box approach. Maybe idea for feature request to have topics backed up by durable store?
I don't think that there is a solution like this available at the moment...
We've done this in our project. It's quite trivial. We've implemented custom ApplicationMessageInterceptor(). Just remember to create scoped DB context from a pool inside (or use DI) and pass the MqttApplicationMessageInterceptorContext.ApplicationMessage.Payload (JSON in our case). We've implemented code that using reflection tries to find method in a given namespace that matches normalized topic (msgType/deviceType/deviceName -> ParseDeviceTypeMsgType()) that handles the payload.
You could also take a look at my project under https://github.com/SeppPenner/NetCoreMQTTExampleCluster. I'm storing incoming messages to a database table (PostgreSQL with TimescaleDB). Specifically, take a look at NetCoreMQTTExampleCluster.Storage and don't be irritated by the clustering stuff which is not necessary for this question.