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

1.6k Views Asked by At

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:

  • desktop web client
  • MQTT Explorer
  • 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:

  • desktop web client
  • MQTT Explorer

But not

  • 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.

UPDATE

Here are the contents of my mosquitto.conf file:

listener 1883
password_file /etc/mosquitto/mypasswordfile

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

Note: I also tried adding the following line to the .conf file:

allow_anonymous true

But that didn't solve anything.

Here’s another thing – possibly a distraction, or maybe there’s a clue in here:

In the past, if I stated Mosquitto with the following command

mosquitto -v

the communications would be outputted to the console (I could see all the PUBLISH and PUBACKS etc.). But if I started the broker with this command

sudo systemctl start mosquitto

I wouldn’t get any output to the console. Which makes sense. But now that I have upgraded to the latest Mosquitto, when I run the command

mosquitto -v

it says it is only starting in local only mode. Even though I have a username/password file defined. And when I start the broker this way I CANNOT connect to it with the web client or MQTT explorer. But when I start it with this command

sudo systemctl start mosquitto

the web client and mqtt explorer CAN connect to the broker, it's just the mqtt.js code that can't. So I don't really understand the difference. However, the fact remains that when starting with

sudo systemctl start mosquitto

my web client and mqtt explorer have no problem on that port etc., but something is still missing from the mqtt.js code.

So, in summary:

After updating to the latest Mosquitto version (and restarting VM and Mosquitto), my mqtt.js code does NOT connect with the broker anymore, but the web client and MQTT Explorer do.

All 3 of these (mqtt.js, web client, mqtt explorer) use the SAME port, the SAME ip address, the SAME user name and password.

The only change I made to the Javascript was to add the username and password. So obviously this is not enough - although I cannot find any documentation about any other changes I need to make.

Any help / suggestions greatly appreciated

1

There are 1 best solutions below

0
On

I solved the issue.

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

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

Note 2: Thank you hardlib for prodding me a few times to get the logs.

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 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).