MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

When connect() is called, it uses localhost and not the host specified in the options object

Open eye-c opened this issue 2 years ago • 6 comments

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

eye-c avatar Feb 11 '23 20:02 eye-c

When adding the host explicitly as the first argument it prepends ws//localhost to the host url MQTTJS ERROR

eye-c avatar Feb 15 '23 23:02 eye-c

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" }

eye-c avatar Feb 16 '23 00:02 eye-c

I think you using wrong protocol value, it's should be "wss" or "ws".

sontran0410 avatar Feb 25 '23 08:02 sontran0410

Try using 127.0.0.1 I triednti connect yo localhost and just problems, but with 127.0.0.1 no problem

lufke avatar Mar 29 '23 21:03 lufke

You should define both host and hostname with the same host server URL.

doc-han avatar Apr 03 '23 18:04 doc-han

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

robertsLando avatar Jul 21 '23 14:07 robertsLando

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.

github-actions[bot] avatar Jul 21 '24 01:07 github-actions[bot]

This issue was automatically closed due to inactivity.

github-actions[bot] avatar Aug 04 '24 01:08 github-actions[bot]