First, the bad news. It seems that the Mosquitto package from Raspbian official repositories doesn’t include web socket support. Good news is that Mosquitto has a partner which provides packages for Raspbian (both Wheezy and Jessie). These packages support the web socket protocol.
Installing Mosquitto with web socket support
The setup is well described in Mosquitto’s blog post. For Raspbian Jessie, here what needs to be done if package was already installed :
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update sudo apt-get dist-upgrade
When I performed dist-upgrade, apt asked me to install libwebsockets. Another way would be to remove mosquitto and install it after.
Configuring Mosquitto to support web socket connexions
Mosquitto can support to listen on several ports, each port having its own protocol. In order to enable web socket protocol, and to keep raw mqtt protocol, you need to declare both protocols.
In /etc/mosquitto/mosquitto.conf, or in a new file (ex: /etc/mosquitto/conf.d/websocket.conf) add the following lines:
listener 1883 listener 1884 protocol websockets
I set first listener to default port and didn’t specified protocol to get default mqtt protocol. Then I chose another port for web socket protocol.
Then restart Mosquitto:
sudo service mosquitto restart
Now first thing to try is that you didn’t break anything. For that, use the commands from my previous post.
A simple web client
The code is pretty simple and I won’t comment it since it is a simple adaptation of the code provided on Paho’s page. Just take care to specify the web socket port, not the MQTT one.
Code source is available here: 2016-05-29_MosquittoWebSockets.