Latest versions of mosquitto (since 1.4) include the websockets interface, but the binary download available from the website does not. So you have to download the source code and build the packet your own, last you have to configure mosquitto to use websockets.

In the following few lines I’m going to show to you how to build and configure mosquitto with websockets on a clean debian wheezy distro.

Step 1 – Let’s start preparing the build system

$ sudo apt-get update
$ sudo apt-get install build-essential python quilt devscripts python-setuptools python3
$ sudo apt-get install libssl-dev
$ sudo apt-get install cmake
$ sudo apt-get install libc-ares-dev
$ sudo apt-get install  uuid-dev
$ sudo apt-get install daemon
Step 2 – Download and build libwebsockets
Latest version of libwebsockets prevents mosquitto to be built, so I’m going to use version 1.4 of libwebsockets:
$ wget https://libwebsockets.org/git/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz
$ tar zxvf libwebsockets-1.4-chrome43-firefox-36.tar.gz
$ cd libwebsockets-1.4-chrome43-firefox-36
$ mkdir build
$ cd build
$ cmake ..
$ make install
$ ldconfig
$ cd
 mosquitto_mqtt
Step 3 – Download and build mosquitto 1.4.2
$ wget http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz
$ tar zxvf mosquitto-1.4.2.tar.gz
$ cd mosquitto-1.4.2
Edit config.mk changing the line
WITH_WEBSOCKETS:=no
to
WITH_WEBSOCKETS:=yes
$ make
$ make install
$ cp mosquitto.conf /etc/mosquitto
Step 4 – Configure mosquitto to use websockets
Add the following lines to /etc/mosquitto/mosquitto.conf in the “Default Listener” section:
port 1883
listener 9001
protocol websockets
So your config file should look like the following:
# =================================================================
# Default listener</code>
# =================================================================
# IP address/hostname to bind the default listener to. If not
# given, the default listener will not be bound to a specific
# address and so will be accessible to all network interfaces.
# bind_address ip-address/host name
#bind_address
# Port to use for the default listener.
port 1883
listener 9001
protocol websockets
Step 5 – Last steps
$ adduser mosquitto
$ reboot
Step 6 – Run mosquitto
$ mosquitto -c /etc/mosquitto/mosquitto.conf
Now you are ready to run your websockets application against mosquitto.
Gg1

If you’ve found useful this post, please, make a visit to my linkedin profile gg1 to help me growing my ranking.