Just for a quick reference, the following instructions detail how to install the latest Mosquitto MQTT broker with Websockets enabled on the ODroid C1+ running (L)Ubuntu release. The instructions are probably also valid for other platforms, but not tested.
1. Install the prerequisites
As the root user, install the following libraries:
apt-get update apt-get install uuid-dev libwebsockets-dev
Probably the SSL libraries, and a few others are also needed, but in my case they where already installed.
2. Mosquitto install
Download and compile the Mosquitto broker:
mkdir ~/mosq cd ~/mosq wget http://mosquitto.org/files/source/mosquitto-1.4.5.tar.gz tar xvzf mosquitto-1.4.5.tar.gz cd mosquitto-1.4.5/
Edit the config.mk file to enable the websockets support:
# Build with websockets support on the broker. WITH_WEBSOCKETS:=yes
and compile and install:
make make install
3. Configuration
Copy the file mosquitto.conf to /usr/local/etc, and edit the file:
cp mosquitto.conf /usr/local/etc cd /usr/local/etc
Add at least the following lines to mosquitto.conf file to enable websockets support.
# Port to use for the default listener. #port 1883 listener 1883 listener 9001 protocol websockets
Add an operating system runtime user for the Mosquitto daemon:
useradd -lm mosquitto cd /usr/local/etc chown mosquitto mosquitto.conf
If needed, or wanted, change also on the configuration file the logging level and destination.
For example:
# Note that if the broker is running as a Windows service it will default to # "log_dest none" and neither stdout nor stderr logging is available. # Use "log_dest none" if you wish to disable logging. log_dest file /var/log/mosquitto.log # If using syslog logging (not on Windows), messages will be logged to the # "daemon" facility by default. Use the log_facility option to choose which of # local0 to local7 to log to instead. The option value should be an integer # value, e.g. "log_facility 5" to use local5. #log_facility # Types of messages to log. Use multiple log_type lines for logging # multiple types of messages. # Possible types are: debug, error, warning, notice, information, # none, subscribe, unsubscribe, websockets, all. # Note that debug type messages are for decoding the incoming/outgoing # network packets. They are not logged in "topics". log_type error log_type warning log_type notice log_type information # Change the websockets logging level. This is a global option, it is not # possible to set per listener. This is an integer that is interpreted by # libwebsockets as a bit mask for its lws_log_levels enum. See the # libwebsockets documentation for more details. "log_type websockets" must also # be enabled. websockets_log_level 0 # If set to true, client connection and disconnection messages will be included # in the log. connection_messages true # If set to true, add a timestamp value to each log message. log_timestamp true
4. Automatic start
The easiest way is to add the following file to the init.d directory and link it to the current runlevel:
Create the file named mosquitto under /etc/init.d
#!/bin/bash case "$1" in start) echo "Starting Mosquitto MQTT Broker" touch /var/log/mosquitto.log chown mosquitto /var/log/mosquitto.log su - mosquitto -c "/usr/local/sbin/mosquitto -c /usr/local/etc/mosquitto.conf" & > /dev/null 2>/dev/null ;; stop) echo "Stopping Mosquitto MQTT Broker" killall mosquitto ;; *) echo "Usage: /etc/init.d/mosquitto start|stop" exit 1 ;; esac exit 0
Find out the current run level. On Odroid it seems that is level 2:
root@odroid:/etc/init.d# runlevel N 2 root@odroid:/etc/init.d#
And link the automatic start for Mosquitto broker at run level 2:
cd /etc/rc2.d ln -s /etc/init.d/mosquitto S97mosquitto
And that’s it.
We can start manually the broker with the command /etc/init.d/mosquitto start, and stop it with the mosquitto stop command.