Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eye-c opened this issue Feb 11, 2023 · 6 comments

Comments

@eye-c
Copy link

eye-c commented Feb 11, 2023

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
Copy link
Author

eye-c commented Feb 15, 2023

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

@eye-c
Copy link
Author

eye-c commented Feb 16, 2023

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

@byterygon
Copy link

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

@lufke
Copy link

lufke commented Mar 29, 2023

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

@doc-han
Copy link

doc-han commented Apr 3, 2023

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

@robertsLando
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants