Mosquitto Build Notes (Linux) - bapowell/bapowell.github.io GitHub Wiki

Build Mosquitto (with websockets) on Linux

Install prerequisite packages

RHEL: sudo yum install c-ares-devel libuuid-devel openssl-devel -y
Ubuntu: sudo apt-get install git cmake libc-ares-dev uuid-dev libssl-dev zlib1g-dev
RasPi: sudo apt-get install git cmake libc-ares-dev uuid-dev libssl-dev daemon

Build libwebsockets

Download libwebsockets source

wget http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.6.2.tar.gz
(also tested with http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.6.0-chrome48-firefox42.tar.gz)
tar xzvf libwebsockets-1.6.2.tar.gz
cd libwebsockets-1.6.2

Run cmake (note the "..")

mkdir build
cd build

RHEL: cmake .. -DLIB_SUFFIX=64
Ubuntu: cmake .. -DLIB_SUFFIX=64
RasPi: cmake ..

Compile and install libwebsockets

sudo make install

RHEL: sudo ln -s /usr/local/lib64/libwebsockets.so.6 /lib64/libwebsockets.so.6
Ubuntu: sudo ln -s /usr/local/lib64/libwebsockets.so.6 /lib/libwebsockets.so.6
RasPi: sudo ldconfig #rebuild the library cache

cd ../..

Build Mosquitto

Download mosquitto 1.4.7 source

wget http://mosquitto.org/files/source/mosquitto-1.4.7.tar.gz
tar xzvf mosquitto-1.4.7.tar.gz
cd mosquitto-1.4.7

Edit config.mk

  • To enable websockets, modify WITH_WEBSOCKETS:=yes
  • If don't want to install dependencies, modify WITH_TLS:=no, WITH_TLS_PSK:=no, WITH_UUID:=no, WITH_SRV:=no

Compile mosquitto

  • If encounter problems running make, and need to re-do some steps, run "make clean" before running make again.

make

  • Compiled mosquitto and mosquitto_passwd executables will be located in the src subdirectory.
  • Compiled mosquitto_pub and mosquitto_sub executables will be located in the client subdirectory.
  • Compiled libmosquitto.* will be located in the lib subdirectory.

Install (if desired)

sudo make install

  • Will install the following:
    • /usr/local/sbin/mosquitto
    • /usr/local/include/mosquitto_plugin.h
    • /usr/local/bin/mosquitto_passwd

Add mosquitto user (if desired)

sudo groupadd mosquitto
sudo useradd -s /sbin/nologin mosquitto -g mosquitto -d /var/lib/mosquitto

Create service (if desired)

  • Note: This will add autostart capability, using SystemD.
  • Install mosquitto ("sudo make install"; see above).
  • Add mosquitto user (see above).
  • Create /etc/systemd/system/mosquitto.service file:
[Unit]
Description=Mosquitto MQTT v3.1/v3.1.1 server
Wants=network.target
Documentation=http://mosquitto.org/documentation/

[Service]
Type=simple
User=mosquitto
Group=mosquitto
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=on-failure
SyslogIdentifier=Mosquitto

[Install]
WantedBy=multi-user.target
  • Create /etc/mosquitto/mosquitto.conf
    • Example content is shown below under the Test section.

sudo systemctl daemon-reload

  • Starting the service:
    • sudo systemctl start mosquitto.service
  • Stopping the service:
    • sudo systemctl stop mosquitto.service
  • To enable Mosquitto to run automatically at boot and upon crashes:
    • sudo systemctl enable mosquitto.service
  • To disable:
    • sudo systemctl disable mosquitto.service
  • For logging, SystemD uses /var/log/system.log. To filter the log use:
    • sudo journalctl -f -u mosquitto -o cat
  • Ref: http://www.dynacont.net/documentation/linux/Useful_SystemD_commands

Test

Create mosquitto_ws.conf

listener 1883
protocol mqtt

listener 9001
protocol websockets
http_dir /home/johndoe/mqtt/ws/http   # note that this folder needs to exist; otherwise mosquitto will choke upon startup

Run

  • Before running, kill any processes using port 1883 or 9001.

  • If installed ("sudo make install"; see above):

    mosquitto -v -c mosquitto_ws.conf

  • If not installed, then from the folder where you built mosquitto:

    src/mosquitto -v -c mosquitto_ws.conf

  • To use mosquitto_sub/pub clients, in separate terminal session, and from the folder where you built mosquitto:

    LD_LIBRARY_PATH=./lib/ client/mosquitto_sub -v -t test
    LD_LIBRARY_PATH=./lib/ client/mosquitto_pub -t test -m "hello"

Paho websockets javascript utility

⚠️ **GitHub.com Fallback** ⚠️