MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Durable session store

Open MarekLani opened this issue 6 years ago • 5 comments

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

MarekLani avatar Nov 22 '19 14:11 MarekLani

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)?

SeppPenner avatar Nov 25 '19 08:11 SeppPenner

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?

MarekLani avatar Nov 25 '19 12:11 MarekLani

I don't think that there is a solution like this available at the moment...

SeppPenner avatar Nov 25 '19 15:11 SeppPenner

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.

Slaviusz avatar Jun 16 '20 06:06 Slaviusz

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.

SeppPenner avatar Jun 16 '20 09:06 SeppPenner