mqttmosquitto

How to configure two Mosquitto listeners with different protocols?


I'm trying to enable websocket on mosquitto 2.0.11 in Ubuntu 22.04. Here my /etc/mosquitto/mosquitto.conf:

pid_file /run/mosquitto/mosquitto.pid
per_listener_settings true
password_file /etc/mosquitto/pwd_mqtt
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
connection_messages true
log_timestamp true

listener 1883
protocol mqtt
allow_anonymous false

listener 8080
protocol websockets
allow_anonymous false

Starting it with: mosquitto -c /etc/mosquitto/mosquitto.conf -v it outputs in /var/log/mosquitto/mosquitto.log:

1707293771: mosquitto version 2.0.11 starting
1707293771: Config loaded from /etc/mosquitto/mosquitto.conf.
1707293771: Opening ipv4 listen socket on port 1883.
1707293771: Opening ipv6 listen socket on port 1883.
1707293771: Opening websockets listen socket on port 8080.
1707293771: Opening ipv4 listen socket on port 1883.
1707293771: Error: Address already in use

Why does it try to open again port 1883 on the second listener?


Solution

  • I've duplicated this with Mosquitto 2.0.18:

    mosquitto-1  | 1707333576: mosquitto version 2.0.18 starting
    mosquitto-1  | 1707333576: Config loaded from /mosquitto/config/mosquitto.conf.
    mosquitto-1  | 1707333576: Opening ipv4 listen socket on port 1883.
    mosquitto-1  | 1707333576: Opening ipv6 listen socket on port 1883.
    mosquitto-1  | 1707333576: Opening websockets listen socket on port 8080.
    mosquitto-1  | 1707333576: Opening ipv4 listen socket on port 1883.
    mosquitto-1  | 1707333576: Error: Address in use
    mosquitto-1 exited with code 
    

    The issue appears to be that password_file is a per_listener setting, but in your config you have it placed above the listener lines, meaning that it applies to the default listener. To solve this either remove per_listener_settings true (it's not needed in your config as it stands), or move the password_file line i.e. (simplified things a little):

    per_listener_settings true
    connection_messages true
    log_timestamp true
    
    listener 1883
    protocol mqtt
    allow_anonymous false
    password_file /mosquitto/config/pwd_mqtt
    
    listener 8080
    protocol websockets
    allow_anonymous false
    password_file /mosquitto/config/pwd_mqtt
    

    See this issue for something similar.