This post describes the steps I used to set up the Mosquitto MQTT broker (server) with websockets enabled on a Raspberry Pi .
Updated November 2 2016
Things have changed quite a bit since this post was first written(August 2015). After receiving a comment that the instructions were not working I went through the installation process and found that it is now much quicker and easier.
There is a Debian repository with the latest Mosquito binary which has been compiled with websockets enabled out of the box.
The instructions which follow have been completely updated and have been tested by installing the Mosquitto MQTT broker on a Raspberry Pi running Raspbian Jessie but should also work on other Debian systems
The first few steps are for adding the Mosquitto Debian repository to your system. They are taken directly from the Mosquitto blog post and are reproduced here (with slight modifications) to keep everything in one place.
To use the new repository you should first import the repository package signing key.
sudo apt-key add mosquitto-repo.gpg.key
To make the repository available to apt:
Then do one of the following, depending on which version of debian you are using.
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
Update apt information.
sudo apt-get update
sudo apt-get install mosquitto
In order to enable websockets you will need to edit a Mosquitto configuration file. There is a default config file located at:
It contains some basic settings and it is best to leave this file as-is. It contains comments that say to “Place your local configuration in /etc/mosquitto/conf.d/” It refers to a file at:
However, I found the file at that location was a gziped file.
Unzip the example file:
sudo gunzip /usr/share/doc/mosquitto/examples/mosquitto.conf.gz
copy the unziped file to the location indicated in the default config file:
sudo cp /usr/share/doc/mosquitto/examples/mosquitto.conf /etc/mosquitto/conf.d/mosquitto.conf
The mosquitto config file is where most of the settings that control mosquitto’s behavior, including security features, live.
It is necessary to change some settings in order for Python and JaveScript to communicate.
Open the mosquitto config file in your favorite text editor
sudo editor /etc/mosquitto/conf.d/mosquitto.conf
As you scroll down through the file you will see many settings. I changed the following (mostly by removing the leading # to uncomment the setting):
line ~43: user mosquitto
The following settings are for the Python (default) client.
line ~133: bind_address 127.0.0.1
line ~136: port 1883
line ~151: protocol mqtt
line ~277: listener 9001 127.0.0.1
line ~297: protocol websockets
If you will be using mosquitto to build a web based UI, you can have it also act as a web server for your pages.
line ~301: http_dir /usr/share/mosquitto/www
Create the directory for the web files:
sudo mkdir -p /usr/share/mosquitto/www
Note: having the same settings in both the default config. file and the local config file seems to cause a conflict that prevents mosquitto from starting. Also, if there is more that one space between a setting name and it’s value, the file won’t load.
Get and set up systemd unit file from GitHub
The following instructions include the installation of a systemd unit file in place of the older init.d script which is used with Raspbian Wheezy. The older init.d script is included with the Mosquitto installation. While the newer systemd which is used in Raspbian Jessie is backward compatible with the init.d script, the systemd unit file is much simpler and more efficient. these steps can also be considered optional.
If you are running Raspbian light you may need to install Git. You can do so with:
sudo apt-get install git
First disable the init.d script:
sudo update-rc.d mosquitto remove
sudo git clone https://github.com/Dan-in-CA/mosquitto_unit_file.git
copy the mosquitto.service file
sudo cp mosquitto_unit_file/mosquitto.service /etc/systemd/system/mosquitto.service
Enable the service
sudo systemctl enable mosquitto.service
After a reboot you can use:
sudo systemctl status | stop | start | restart mosquitto
to control the mosquitto daemon. This is extremely useful when editing and testing the mosquitto.conf file.
The systemd unit file also tells mosquitto which config file to use. Otherwise the mosquitto daemon uses defalult settings which won’t work for our intended purpose. You can test mosquitto using the default settings with:
This will tell you the version of mosquitto you are running.
I did not set up any security or other options but it is a good idea to read through the file to learn what is available.
Notes on usage:
The Python client communicates with the mosquitto broker using the default MQTT protocol and port 1883.
A common mistake is to try and use port 9001 from Python. This will cause Python to hang without raising an exception and can be hard to debug.