Connecting to Mosquitto broker using mqtt.js with username and password

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 :slight_smile:

Note: the formatting of the above post might suggest that I had entered a ‘>’ before the username/password in my JavaScipt code. I haven’t - I just typed the post incoreectly.
The connect option is:

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

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:

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 or false, 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).

I’m sorry Garrett, I thought I’d replied to this topic earlier.

You can set max_keepalive 0 in the mosquitto config to allow an infinite keepalive.

I will be reverting the need for this in 2.1 - it seems to affect too many people to have it set to 65535 by default.

Regards,

Roger