0

I'm coding an application in blazor using MQTTNET nuget to connect to hivemq (a public free server)

If I connect without WithTls() It does work when debugging locally but once I upload to github pages it fails because "a https tried to connect to a ws server, it should connect to wss instead".

If I add the Tls it does not connect: Unable to connect to the remote server I tried searching if hivemq has a different address for wss or something but could not find out.

This is the code I'm using, mostly copied from the MQTTNET wiki

mqttClientOptions = new MqttClientOptionsBuilder()
                    .WithClientId(ClientID)
                    .WithWebSocketServer("broker.hivemq.com:8000/mqtt")
                    .WithTls() //with Tls it does not connect, but no error is thrown
                    .Build();

var result = await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

javirs
  • 1,049
  • 26
  • 52
  • The call is made by the browser, which uses TLS by default. Your own page is served through HTTPS so it *can't* connect to unencrypted pages. – Panagiotis Kanavos Jan 10 '23 at 14:45
  • What you tried to do is serve [mixed content](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content) which is [blocked by browsers](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content/How_to_fix_website_with_mixed_content) – Panagiotis Kanavos Jan 10 '23 at 14:48

1 Answers1

2

If you add .WithTls() you will also need to change the port number.

From HiveMQ's website:

MQTT connection settings 
Host: broker.hivemq.com 
TCP Port: 1883
Websocket Port: 8000 
TLS TCP Port: 8883 
TLS Websocket Port: 8884
  • 8000 for plain ws
  • 8884 for wss
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • It's worth noting that the call is made by the browser, from a GitHub page served through HTTPS. Mixed content like this is blocked by all browsers, even Internet Explorer – Panagiotis Kanavos Jan 10 '23 at 14:46