MQTTnet
MQTTnet copied to clipboard
Question: Optional message when API controller receives request
Describe your question
Hello
I'm a bit confused about the recommended usage of the MQTTnet clients for my use case.
The way I see it, I would like to publish a message, when a specific endpoint is triggered in my API. However, I do not want my API to be dependent on the (external) MQTT server being available.
One approach is to use the ManagedClient. But looking at your examples and documentation, it looks like StartAsync(...) would be invoked during startup, which means my API would require the MQTT broker to be available on startup, no? Is there any way I could avoid this?
Alternatively, I could use the Client, and just create a new each time, waiting for the connect, then send the message, before finally disconnecting.
Do you have any recommendations as to which approach to use?
Which project is your question related to?
- Client
- ManagedClient
The client will require a working connection. So you have to queue your data on your own which is the recommended way. For this an Azure Queue or similar can be used.
If you want to do this with this library you can use the managed client. It will be started only. The connection is made internally as soon as the server is available. The managed client will also reconnect on its own. That's why you just "Start" it instead of connecting. The messages are also just enqueued to the managed client instead of published. The effective upload will be performed later by the managed client. This can be very quickly if the managed client is already connected. Otherwise the message gets enqueued and nothing more happens.
I recommend to check the storage implementation in the options. If you pass an implementation the managed client is able to persist its queued data. Without an implemented all queued messages are lost on application restarts etc.