dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.45k stars 1.06k forks source link

Question: Using MQTT client in .NET Core WebAPI #391

Closed ksami closed 6 years ago

ksami commented 6 years ago

Hi, what is the recommended way of using the MQTT client in a .NET Core 2 WebAPI? Eg. creating a new instance of client for every request or reusing the same instance? Would using ManagedClient help in this case?

JanEggers commented 6 years ago

why would you add a client on the server?

you can use mqtt.js directly in the browser to connect to a mqtt broker.

ksami commented 6 years ago

The consumer for the API is not a browser. In my case, the API acts as a webhook for a server in a 3rd party system to call, which will then trigger updates to be sent to MQTT subscribers. Do you know what is the recommendation for using the MQTT client in this way?

JanEggers commented 6 years ago

if you dont need to hanle incoming messages but just send messages if an api is called i would create a new client for each method call.

ksami commented 6 years ago

Thanks! There might be a requirement in the future to receive messages too to process updates from the subscribers. In that case, would it still be better to create a new client for each request?

JanEggers commented 6 years ago

in case you need to receive messages you can create an IHostedService that has its own client and processes the messages.

ksami commented 6 years ago

Huh why didn't I think of that... Thanks for the help!