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.46k stars 1.06k forks source link

Broker Server: How to use into a web .NET Core 2 / Standard proejct? #206

Closed biapar closed 6 years ago

biapar commented 6 years ago

How to use Broker Server into a web .NET Core 2 / Standard proejct? Or better to build a "standard" or "console" application for only broker? If yes, I think that I've to use MQTTClient into web project to connect to MQTT Broker. Is right?

rshillington commented 6 years ago

I think what you're looking for are two projects, one to be the server, and one to be the client.

If you're wanting to use WebSockets then you'll need the server project to be an ASP.NET Core and you'll need to reference the mqttnet.aspnetcore package as well as mqttnet

biapar commented 6 years ago

My idea is to build and have a .net standard broker server and the client is web/rest to present data to the customer and send commands. Do you suggest to have a web broker server if there are a lots iot clients?

JanEggers commented 6 years ago

I would use mqtt like in the sample project with mqttjs in the browser, why would you want to wrap that in webapi? maybe create a asyncapispec document so your customers have docs how to implement the interface.

if you have many clients i would use hivemq, azure iot hub or similar. this package does not have a skaleout mechanic so if a single server cant handle the load you are screwed. (Sidenote: I switched to UTF8.Json for payload serialization because for my messages that was 10 times faster than Json.Net)

im currently playing around with signalr / creating a mqtt transport for signalr but that is just prototyping stage.

biapar commented 6 years ago

Hi, thanks. Azure iot is planned in the future, but I need something that we can “control”. Do you suggest that client talk direct to mqqt server without passing trough web api core? At this stage I integrated signalR into web api core 2 and send info to Angular 5 client.

JanEggers commented 6 years ago

Do you suggest that client talk direct to mqqt server without passing trough web api core?

yup exactly

biapar commented 6 years ago

Ok. I’ll try your suggestion and use web api for the other bussines api need by client. You can made a push/ pull with SignalR Hub or Socket.io.

JanEggers commented 6 years ago

you dont need signalr there is a websocket mqtt js library to publish messages or subscribe to updates.

https://github.com/chkr1011/MQTTnet/blob/54a0265ef9ca6ce82efdb4d6d3d061d3fad70b5f/Tests/MQTTnet.TestApp.AspNetCore2/package.json#L9

https://github.com/chkr1011/MQTTnet/blob/54a0265ef9ca6ce82efdb4d6d3d061d3fad70b5f/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts#L3-L56

biapar commented 6 years ago

Yes, if you don’pass trough web api. I'm using it to send real-time info to client. One MQTT benefit is to retain the messages if the client is offline.

Maybe this interesting: Redis + SignalR