Skip to content

Setup a mosquitto server with websocket support

Åke Hedman edited this page Aug 20, 2021 · 2 revisions

Setup a mosquitto server with websocket support

On many distributions the mosquitto server is old and also it misses websocket support. This means the

apt install mosquitto

is not an option but that one instead need to install the package oneself. This mostly easy but as there are some culprits I go through the process here in a step by step fachon.

Get and build the libwebsocket package

Mosquitto use this package for it's websocket support so it needs to be installed on your system.

Config file

For me the mosquitto configuration file format is a bit strange but after some guessing I think I got most of it.

First of all the standard location for the file is in

/etc/mosquitto/mosquitto.conf

we use that location here.

First set

per_listener_settings true

at the top of the file. If set all security related things is set per listeners. This means that if you define users that have access to the system in a file this file must be set for all listeners if the same credentials is needed for all, or pointers to different files if not the same. We will see this later.

Set

retain_available true

if you want to accept retained messages. I do.

Set

set_tcp_nodelay false

to disable Nagle's algorithm. This gives better performance for short messages like VSCP events. Don't do this if you work with large messages also.

Define the listeners

listener 1883
allow_anonymous false
password_file /etc/mosquitto/password

#listener 8883
#cafile /etc/mosquitto/certs/ca.crt
#certfile /etc/mosquitto/certs/server.crt
#keyfile /etc/mosquitto/certs/server.key
#include_dir /etc/mosquitto/conf.d

listener 9001
protocol websockets
allow_anonymous false
password_file /etc/mosquitto/password
websockets_log_level all

Here we have both tcp/ip setup and websocket. Both need credentials from the /etc/mosquitto/password file. Set allow_anonymous to true if you want to accept credential less connections.

For TLS setup see below.

If you want to autostart your mosquitto server you should set a path to a pid-file

pid_file /var/run/mosquitto/mosquitto.pid

This is it really, but you may want to setup persistence like this

autosave_interval 1800
autosave_on_changes false
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/

This means that persistent data will be saved every 1800 seconds (every half hour) to the file /var/lib/mosquitto/mosquitto.db

Also you may set up logging

log_dest stderr
log_dest topic
log_dest file /var/log/mosquitto/mqtt.log
log_dest syslog

log_type error
log_type warning
log_type notice
log_type information
log_type websockets

log_facility 5

websockets_log_level 0xffff

Create a mosquitto service file for systemv

[Unit]
Description=Mosquitto MQTT Broker
Documentation=man:mosquitto.conf(5) man:mosquitto(8)
After=network.target
Wants=network.target

[Service]
#Type=notify
#NotifyAccess=main
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

[Install]
WantedBy=multi-user.target

name it mosquitto.service and save it in /etc/systemv/system

Clone this wiki locally