Skip to main content

Building Eclipse Mosquitto v2 from Source

Guangzhou, China

Building from source on Arch LINUX

To build from source the recommended route for end users is to download the archive from mosquitto.org. On Windows and Mac, use cmake to build. On other platforms, just run make to build. If you are building from the git repository then the documentation will not already be built. Use make binary to skip building the man pages.

Setup

mkdir mosquitto && cd mosquitto
wget https://mosquitto.org/files/source/mosquitto-2.0.14.tar.gz
tar -xvzf mosquitto-2.0.14.tar.gz
cd mosquitto-2.0.14

Build Dependencies

  • c-ares (libc-ares-dev on Debian based systems): Only when compiled with make WITH_SRV=yes
  • cJSON for client JSON output support: Disable with make WITH_CJSON=no Auto detected with CMake.
  • libwebsockets (libwebsockets-dev): Enable with make WITH_WEBSOCKETS=yes
  • openssl (libssl-dev on Debian based systems): Disable with make WITH_TLS=no
  • pthreads: for client library thread support: This is required to support the mosquitto_loop_start() and mosquitto_loop_stop() functions. If compiled without pthread support, the library isn't guaranteed to be thread safe.
  • uthash / utlist: Bundled versions of these headers are provided, disable their use with make WITH_BUNDLED_DEPS=no
  • xsltproc (xsltproc and docbook-xsl on Debian based systems): Only needed when building from git sources - disable with make WITH_DOCS=no

Equivalent options for enabling/disabling features are available when using the CMake build.

sudo pacman -S c-ares cjson libwebsockets openssl uthash

Packages (5) c-ares-1.18.1-1 cjson-1.7.15-1 libwebsockets-4.3.1-1 openssl-1.1.1.o-1 uthash-2.3.0-1

Build

Make the changes to ./config.mk as required, e.g.:

# Build with SRV lookup support.
WITH_SRV:=yes
# Build with websockets support on the broker.
WITH_WEBSOCKETS:=yes
# Comment out to disable SSL/TLS support in the broker and client.
# Disabling this will also mean that passwords must be stored in plain text. It
# is strongly recommended that you only disable WITH_TLS if you are not using
# password authentication at all.
WITH_TLS:=yes
# Build with bundled uthash.h
WITH_BUNDLED_DEPS:=yes
# Build man page documentation by default.
WITH_DOCS:=no
make -f Makefile binary

The binary will be created in ./mosquitto/mosquitto-2.0.14/src/mosquitto.

Testing

Download the setup environment and copy&paste the binary into it:

git clone https://github.com/mpolinowski/mosquitto-v2-configuration.git

Adjust the absolute file paths inside ./conf.d/custom.conf:

# =================================================================
# General configuration
# =================================================================
auto_id_prefix zeroid-
persistent_client_expiration 1d
queue_qos0_messages true
user mosquitto
# =================================================================
# Listeners
# =================================================================
# MQTT
listener 1883
protocol mqtt
## WS
listener 1885
protocol websockets
socket_domain ipv4
# -----------------------------------------------------------------
# Certificate based SSL/TLS support
# -----------------------------------------------------------------
#use_identity_as_username false
# MQTT
listener 8883
protocol mqtt
## WS
listener 8885
protocol websockets
tls_version tlsv1.2
cafile /path/to/repo/mqtt-broker/ca_certificates/ca.crt
certfile /path/to/repo/mqtt-broker/certs/client.crt
keyfile /path/to/repo/mqtt-broker/certs/client.key
require_certificate false
# =================================================================
# Persistence
# =================================================================
persistence true
persistence_file mosquitto.db
persistence_location /path/to/repo/mqtt-broker/store
# =================================================================
# Logging
# =================================================================
#log_dest stderr
log_dest file /path/to/repo/mqtt-broker/log/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S
# =================================================================
# Security
# =================================================================
#clientid_prefixes
allow_anonymous false
# -----------------------------------------------------------------
# Default authentication and topic access control
# -----------------------------------------------------------------
password_file /path/to/repo/mqtt-broker/passwordfile
# acl_file /path/to/repo/mqtt-broker/acl.file

And run the binary with:

mosquitto -c ./mosquitto.conf

Connect Clients

The ./passwordfile was created with the user login admin/instar and can be re-created with your own password using ./mosquitto_passwd. The configuration file above brings up 4 listeners:

  • 1883 for MQTT
  • 8883 for MQTT with SSL
  • 1885 for WS
  • 8885 for WS with SSL

Read Mosquitto v2 MQTT Broker on Debian Bullseye for details on how to use the included sample certificate or create your own.

Installing Eclipse Mosquitto from Source