Mqttnet: Mqtt Broker Compatible with Web Socket

Created on 10 Aug 2018  路  15Comments  路  Source: chkr1011/MQTTnet

Hi ,
谋 want to make 3 application

  1. mqtt broker (C# console app)
static void Main(string[] args)
        {
            Console.WriteLine("Start Broker!");

            // Configure MQTT server.
            // Configure MQTT server.
            var optionsBuilder = new MqttServerOptionsBuilder()
                .WithConnectionBacklog(100)
                .WithDefaultEndpointPort(1883);
            // Start a MQTT server.
            var mqttServer = new MqttFactory().CreateMqttServer();
            mqttServer.StartAsync(optionsBuilder.Build());
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
            mqttServer.StopAsync();
        }
  1. mqtt client (C# console app)
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                LocationName = args[0].Trim();
            }
            else
                LocationName = "L1";


            Console.WriteLine("Hello World!");


            // Create a new MQTT client.
            var factory = new MqttFactory();
            mqttClient = factory.CreateMqttClient();

            //// Use WebSocket connection.
            //var options = new MqttClientOptionsBuilder()
            //     .WithClientId("Client1")
            //    .WithWebSocketServer("localhost:1884/mqtt")
            //    .Build();
            // Use TCP connection.
            var options = new MqttClientOptionsBuilder()
                .WithTcpServer("localhost", 1883) // Port is optional
                //.WithWebSocketServer("ws://localhost:8000/mqtt")
                //.WithWebSocketServer("ws://localhost:9001/mqtt")
                 //.WithWebSocketServer("ws://broker.mqttdashboard.com:8000/mqtt")
                .Build();
            mqttClient.ConnectAsync(options);
            mqttClient.Connected += MqttClient_Connected;
            mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;
            mqttClient.Disconnected += MqttClient_Disconnected;



            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
  1. mqtt client (web app)
var ip = "127.0.0.1";
var port = 1883;
var mqttWebClientAddress = 'ws://' + ip + ':' + port ;
var topics = ["L1", "L2", "L3", "L4"];
//var options = {};
//options.protocol = "tcp";
//var options = { "protocol": "tcp:", "slashes": true, "auth": null, "host": "127.0.0.1:1884", "port": "1884", "hostname": "127.0.0.1", "hash": null, "search": "", "query": {}, "pathname": null, "path": null, "href": "tcp://127.0.0.1:1884" }

var mqttWebClient = mqtt.connect(mqttWebClientAddress)
mqttWebClient.on('error', function (error) {
console.log(error)
});

mqttWebClient.on('connect', function () {

    var mqttWebClientId = mqttWebClient.options.clientId;
    console.log(mqttWebClientId + " connected to broker");

    for (var i = 0; i < topics.length; i++) {
        var topic = topics[i];
        mqttWebClient.subscribe(topic, { qos: 1 }, function (err, granted) {
            if (err)
                console.log(topic + " : "+ err);
            else
                console.log(topic + " : "+ granted);
        });
    }
});


mqttWebClient.on('message', function (topic, message) {
        if (message != "") {

                console.log(topic+" --> "+message);
        }
});

mqtt client work fine but mqtt web client didnt work 谋 could not connect mqtt broker via browser because mqtt broker not compatible with web socket connection . 谋 want to connect mqtt broker via browser.

can you help me about this problem ?
Thank you so much
Best Regards

bug duplicate

Most helpful comment

@JanEggers We need to do some further investigation here.

All 15 comments

Hi

WebSockets supported (via ASP.NET Core 2.0, separate nuget)
Not console app.

Peter

as @strov said get the mqttnet.aspnetcore nuget and put it into a console app and done.

start with a template to generate a aspnet core console app:
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore21

then add mqtt websocket support:
https://github.com/chkr1011/MQTTnet/wiki/Server#aspnet-core-integration

Hey @JanEggers, I'm using .net core but this sadly does not work the same way. Is there any way to get the MQTT server with websockets working on .net core?

I currently have this error:
The type 'IMqttServerOptions' is defined in an assembly that is not referenced. You must add a reference to assembly 'MQTTnet, Version=3.0.3.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7'.

EDIT: Not relevant for me anymore, the issue is still there, though

@kevinsnijder can you please create a new issue and post a screenshot of your dependency graph? mqttnet should be included there. maybe there is something wrong in the nuget of 3.0.3

Just updated to the 3.0.3 and got the same error message of @kevinsnijder
The only way I got is to revert to the 3.0.2.

I was just using MQTTnet.AspNetCore 3.0.3. I also tried to add MQTTnet 3.0.3 (just to be sure), but nothing changed.

@JanEggers We need to do some further investigation here.

Just updated to the 3.0.3 and got the same error message of @kevinsnijder
The only way I got is to revert to the 3.0.2.

I was just using MQTTnet.AspNetCore 3.0.3. I also tried to add MQTTnet 3.0.3 (just to be sure), but nothing changed.

i have same error after update to 3.0.3 on .Net Standard 2.0
another project on Net.Framework 4.6 work correct.

Did you maybe added the extension for ASP.NET manually? So is it listed as 3.0.2 in your nuget package list. If it is you must also update it to 3.0.3. If it is not there then we might have another problem.

Having same problem. Here is my csproj:
<PackageReference Include="MQTTnet" Version="3.0.3" /> <PackageReference Include="MQTTnet.AspNetCore" Version="3.0.3" />

I also have same problem.
You must add a reference to assembly 'MQTTnet, Version=3.0.2.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7'
also in Version 3.0.3.

I will close this issue. Please check https://github.com/chkr1011/MQTTnet/issues/677 for further reference/ discussion because these seem to be duplicates.

I am using .NET Core 3.1 for MQTTnet broker service. It's works fine when a client connect with mqtt protocol from MQTT.fx or .NET client; but whenever a request come from browser using ws protocol its seems like this


My .NET broker configuration

startup.cs


program.cs


I saw this issue conversation but nothing found useful for me.

@rafiulgits This is resolved now in the original question under https://github.com/chkr1011/MQTTnet/issues/857, isn't it?

@SeppPenner Yes.

@asimturgut did u find the way to fix it? i have the same problem.

Was this page helpful?
0 / 5 - 0 ratings