Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » Connecting to broker with username and password using mqtt.js(Cannot connect to broker after upgrading to latest Mosquitto version)
Connecting to broker with username and password using mqtt.js [message #1856416] Thu, 08 December 2022 20:36 Go to next message
Garrett Brennan is currently offline Garrett BrennanFriend
Messages: 4
Registered: December 2022
Junior Member
Hi

I am using JavaScript / mqtt.js to connect to a Mosquitto broker.
I had everything working, and then I upgraded to the Mosquitto version 2.0.15, and now I cannot connect to the broker.
Could you read the following, and see if you have any suggestions, plase.

Thanks
Garrett

------------
OLD SET UP
------------

I had v1.6.9 Mosquitto installed and running on Ubuntu (in Google Cloud).
I was able to communicate with the broker using:
(a) desktop web client
(b) MQTT Explorer
(c) JavaScript using mqtt.js

------------
NEW SETUP
------------

I upgraded to latest Mosquitto version 2.0.15.
I had to create a username and password, so I used the instructions here:
https://mosquitto.org/documentation/authentication-methods/

I edited the mosquito.conf file, so it knows about the password file.

I restarted the VM and then started Mosquitto.

------------
WHAT STILL WORKS
------------

Now, I am able to communicate with the broker (with username/password) using:
(a) desktop web client
(b) MQTT Explorer
But not
(c) JavaScript using mqtt.js

--------------------------
mqtt.js connect options
--------------------------

In JavaScript, my connect options used to be of the form (for v1.6.9):

const connection_options = {
port: 1883,
host: host,
clientId: client_id,
clean: true,
keepalive: false,
reconnectPeriod: 5000
};

where host is the IP address of the machine

And are now of the form (for v2.0.15):

const connection_options = {
port: 1883,
host: host,
username: 'user',
password: 'password',
clientId: client_id,
clean: true,
keepalive: false,
reconnectPeriod: 5000
};

I have double checked the username / password values etc.

So, the only thing I changed was adding the username and password, but I can no longer connect with the broker.
The desktop web client, MQTT Explorer and the JavaScript/mqtt.js are all using the same IP address, same port, same username and password.

Any help / suggestions greatly appreciated
Re: Connecting to broker with username and password using mqtt.js [message #1856436 is a reply to message #1856416] Fri, 09 December 2022 19:32 Go to previous message
Garrett Brennan is currently offline Garrett BrennanFriend
Messages: 4
Registered: December 2022
Junior Member
I solved the issue.

Once I turned logging on, I got some very helpful information

Note: I hadn't turned logging on because I an new to mosquitto, linux, mqtt - and I hadn't worked out how to do it!

What I saw, when I tried to connect with mqtt.js was:

Bad socket read/write on client my_publisher: Invalid arguments provided.

I found this on the github forum:
https://github.com/eclipse/mosquitto/issues/2462

I have only skimmed through it I will re-read the full thing later.
The issue is the keep alive (seconds) value.

If it is set 0, I couldn't connect.
Once I set it to a non-zero value, I can connect.

I think there is a different way to get around this, a setting to allow keep alive to be zero, but I will look into this later.

I think it only affects certain versions of Mosquitto (and obviously the latest version).
Previous Topic:Broker not sending all retained messages
Next Topic:Homey to HASS
Goto Forum:
  


Current Time: Sat Apr 20 13:25:57 GMT 2024

Powered by FUDForum. Page generated in 0.03462 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top