blazor mqttnet connection fails due to ws vs wss

269 Views Asked by At

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);

1

There are 1 best solutions below

1
On BEST ANSWER

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