eclipse-paho / paho.mqtt.golang

Other
2.77k stars 534 forks source link

Connection over wss fails #559

Closed ARao0123 closed 2 years ago

ARao0123 commented 2 years ago

Hi, this is my first time using MQTT and I'm trying to use websockets. I tried to connect to mqtt broker and subscribe to a topic via mqttbox and I can do it successfully. But when I try the same thing via code, I get timeout error. Details shown below:

Expected behavior: Successful connection to the mqtt broker, no errors Actual behavior: "mqtt client encounter error connecting: network Error : dial tcp x.x.x.x:XXXX: i/o timeout"

Code snippet:

broker := fmt.Sprintf("wss://%s:%d/mqtt", "address", 1234)

//Create the broker
opts = mqtt.NewClientOptions()
opts.AddBroker(broker)
opts.SetClientID(fmt.Sprintf("clientID.%s", time.Now()))
opts.SetUsername(Username)
opts.SetPassword(Password)

mqttClient = mqtt.NewClient(opts)

if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
    fmt.Println(err)      // This is the error point I hit
}

I've tried the broker with wss and ws, no luck! Any help/suggestions would be greatly appreciated. Thanks in advance!

MattBrittan commented 2 years ago

As per the readme the issues section is intended for use reporting bugs rather than for requesting general assistance (other resources are suggested for that).

However a dial timeout is a network issue; this would generally indicate that there is nothing at the address (or perhaps a firewall is blocking access). I'd suggest trying with the mosquitto test server; if that works OK then it points towards a network/broker issue. If you are still having issues I'd suggest asking on Stackoverflow but you will need to provide full details (i.e. don't hide the addesss/port, detail exactly how you are connecting with mqttbox and provide info on the broker you are using).

ARao0123 commented 2 years ago

Thanks for the suggestion, closing this issue.