Lars Flieger
Lars Flieger

Reputation: 2534

Try to connect to Mosquitto MQTT over WebSockets with Mqtt.js

I want to build a web page to send and receive topics. I'm using mosquitto and MQTT.js.

  1. I'm using this as an config file:
    protocol websockets
    listener 1884
    
  2. I run sudo mosquitto -c /etc/mosquitto/mosquitto.conf and get
    1647529861: mosquitto version 2.0.14 starting
    1647529861: Config loaded from /etc/mosquitto/mosquitto.conf.
    1647529861: Opening websockets listen socket on port 1884.
    1647529861: mosquitto version 2.0.14 running
    
  3. I'm using a simple mqtt.html (full source):
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
    <script>
    const client = mqtt.connect("ws://localhost:1884")
    </script>
    

Problem:

I'm running on:

LSB Version:    n/a
Distributor ID: ManjaroLinux
Description:    Manjaro Linux
Release:    21.2.5
Codename:   Qonos

Upvotes: 1

Views: 1151

Answers (1)

hardillb
hardillb

Reputation: 59608

You need to add allow_annonymous true to your config file.

The default for mosquitto 2.x is to not allow connections from unauthenticated clients.

You should also reverse the lines in your config,

The protocol option only applies to the last listener in order down the file.

What you have will apply the websocket protocol to the default listener on port 1883.

allow_anonymous true
listener 1884
protocol websockets

Upvotes: 5

Related Questions