question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mqtt Broker Compatible with Web Socket

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SeppPennercommented, Jun 25, 2019

@JanEggers We need to do some further investigation here.

1reaction
rafiulgitscommented, Feb 12, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using MQTT Over WebSockets with Mosquitto
In the case of MQTT over Websockets the websockets connection forms an outer pipe for the MQTT protocol. The MQTT broker places the...
Read more >
A Quickstart Guide to Using MQTT over WebSocket - EMQ
This quickstart guide covers the basics of using MQTT over WebSocket to establish real-time communication between MQTT brokers and web ...
Read more >
How to configure MQTT over WebSockets with Mosquitto
How to Enable WebSockets for Mosquitto MQTT Broker · Step 1: Initial installation of dependencies · Step 2: Configure Mosquitto to use WebSocket....
Read more >
How to enable Websockets in Mosquitto MQTT broker?
This post will show you how you can configure your Mosquitto broker to support MQTT over WebSockets.
Read more >
MQTT over WebSockets - MQTT Essentials Special
To communicate with an MQTT broker over WebSockets, the broker must be able to handle native WebSockets. Occasionally, people use a webserver ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found