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