MQTT.js not connecting to Mosquitto on HASS

I developed an app that communicates to HASS via MQTT.js, but I can’t get it to work on the HASS Mosquitto. It connects fine on a local Mosquitto server, but not on the one on HASS. The configuration on HASS is

logins:
  - username: myusername
    password: mypassword
customize:
  active: false
  folder: mosquitto
certfile: /ssl/fullchain.pem
keyfile: /ssl/privkey.pem
require_certificate: false
anonymous: false

And my app uses this code to connect:

var client = mqtt.connect({
    host: 'ws://192.168.1.28',
    port: 1884,
    username: 'myusername',
    password: 'mypassword'
});

I checked that the port is right and that the user/pass are correct.

Why do you use a websocket address for an mqtt connection? Shouldn’t it be mqtt://192.168.1.28?

I looked at the configuration and it showed that the port 1884 is for MQTT over websockets.

I also tried mqtt:// on port 1883 but it still doesn’t want to connect, it doesn’t even show anything on the logs on Mosquitto.

Here my config in HA:
in configuration.yaml:
mqtt:
broker: !secret mqtt_url
port: !secret mqtt_port
client_id: home-assistant
discovery: true
discovery_prefix: homeassistant

in secrets.yaml:

mqtt_url: 192.168.1.45 (put here your mqtt server IP)
mqtt_port: 1883

Did you chevk that the broker is running correctly? Can you connect to the broker with your desktop/laptop?

What happens if you install a linux on a usb stick and then start that and install Mosquitto on that?
You can hen check that the setup is correct and that you an connect to that.

The hard part with Home Assistant and Mosquitto is that you are not really sure what the configuration actually is.

He already pointed out that he can connect to another mosquitto broker, just not the one on Home Assistant.

A connection to a local Mosquitto does not test making the connection over the network and a local Mosquitto is alot less restricted than a Mosquitto open to the network.

The note about not seeing anything in the logs on the remote Mosquitto could mean that there were either a setup issue or a network issue.
Connecting to a Mosquitto on a USB-stick with Linux would rule out the netowrk issue.

Can you connect to the broker using telnet?

telnet <ip of ha> 1883 (or 1884)

Yes on both ports

I did some tests with MQTTX, a desktop MQTT client. I can connect to both my local Mosquitto server and the HASS one on both 1883 and 1884, with MQTT.js, running on the exact same computer, I can connect to the local server only using websockets, and can’t connect at all to HASS. Seems like a MQTT.js problem to me.

I found this guide and it mentions sometinh with protocol ID and protocol version.