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

Closed
eye-c opened this issue Feb 11, 2023 · 8 comments
Labels

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

@sontran0410
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

Copy link

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 github-actions bot added the stale label Jul 21, 2024
Copy link

github-actions bot commented Aug 4, 2024

This issue was automatically closed due to inactivity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants