MQTT.js
MQTT.js copied to clipboard
When connect() is called, it uses localhost and not the host specified in the options object
Hi, I'm running MQTT.js on my dev machine in a react component and have been connecting to my local mosquitto broker without a problem using { host: localhost }. When I try to connect to my HiveMQ broker I get this error:
ws.js?725c:109 WebSocket connection to 'ws://localhost:8884/' failed:
these are my options passed in to the connect function
mqttOptions: {
"host": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud",
"port": "8884",
"clientId": "icodegfx",
"username": "icodegfx",
"password": "password",
"protocol": "MQTT"
}
const mqttConnect = (mqttOptions) => {
console.log(`mqttOptions: ${JSON.stringify(mqttOptions, null, 4)}`)
setConnectStatus('Connecting')
setClient(mqtt.connect(mqttOptions))
}
I don't understand why the client tries to connect to ws://localhost
When adding the host explicitly as the first argument it prepends ws//localhost to the host url

the options object looks like this in the console. mqttOptions: { "host": "58xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0c.s1.eu.hivemq.cloud", "port": "8883", "clientId": "icode-id", "username": "icode", "password": "password", "protocol": "MQTT" }
I think you using wrong protocol value, it's should be "wss" or "ws".
Try using 127.0.0.1 I triednti connect yo localhost and just problems, but with 127.0.0.1 no problem
You should define both host and hostname with the same host server URL.
MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues
This is an automated message to let you know that this issue has gone 365 days without any activity. In order to ensure that we work on issues that still matter, this issue will be closed in 14 days.
If this issue is still important, you can simply comment with a "bump" to keep it open.
Thank you for your contribution.
This issue was automatically closed due to inactivity.