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
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.
Thank you!
Was struggling over this, but your post sorted it all out.
Jim
I’m happy this helps.
Great guide and very easy to follow!
Do you know if there is an easy way to update mosquitto after installment?
Or I guess maybe, due to the libwebsockets, you have to rebuild mosquitto.
In this case we are not using a package manager, so you have to rebuild mosquitto.
after done all of steps without errors when I run
mosquitto -c /etc/mosquitto/mosquitto.conf
I got error below:
bash: mosquitto: command not found
there is no mosquitto in my machine. please let me know how to fix it
Hi Tommy,
are you sure the “make install” commands were executed fine?
try issueing them using sudo
$ sudo make install
hi admin,
I found out problem, because I used root user, so I can not execute mosquitto, to execute it, I need to add user.
any ways thank you.
Tommy
Nice to hear that!
Hi,
I get the following error when I try to execute make command:
set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory ‘/home/pi/mosquitto-1.4.7/lib’
cc -Wall -ggdb -O2 -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -DWITH_SRV -c srv_mosq.c -o srv_mosq.o
In file included from /usr/include/arm-linux-gnueabihf/sys/param.h:31:0,
from /usr/include/arpa/nameser.h:59,
from srv_mosq.c:20:
/usr/include/arm-linux-gnueabihf/bits/param.h:28:25: fatal error: /usr/include/linux/param.h: Input/output error
#include
^
compilation terminated.
Makefile:84: recipe for target ‘srv_mosq.o’ failed
make[1]: *** [srv_mosq.o] Error 1
make[1]: Leaving directory ‘/home/pi/mosquitto-1.4.7/lib’
Makefile:21: recipe for target ‘mosquitto’ failed
make: *** [mosquitto] Error 2
I’ve just compiled version 1.4.7 running the above procedure on debian wheezy. It works fine.
For your problem…
it seems you have to install linux-libc-dev on your raspberry pi distro.
Hi,
I’m getting errors
1457431305: Config loaded from /etc/mosquitto/mosquitto.conf.
1457431305: Opening websockets listen socket on port 9001.
Segmentation fault (core dumped)
After running step 6. I’m not sure that because I’ve skipped Step 5 is the cause or not. Has anyone experienced this error yet ?
Thank you
it seems strange, but what happens if you run the 5th step?
‘libwebsockets-1.4-chrome43-firefox-36.tar.gz’ while extracting gives unexpectec end of file error. Will it work with any other version? I tried downloading latest libwebsocket from github but it gives error while ‘make’ and ‘make install’ of Mosquitto??
Please help!
How i must run this as start up ???
Hi,
after I followed and done all steps.
it shows
root@raspberrypi:/home/pi# mosquitto -c /etc/mosquitto/mosquitto.conf
Error: Websockets support not available.
Error found at /etc/mosquitto/mosquitto.conf:138.
Error: Unable to open configuration file.
what should I do now? (I’m a starter in linux sorry).
couldn’t get through the set up. I got to:
wget http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz with a response of:
http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1
connecting to git.libwebsockets.org (git.libwebsockets.org)|163.172.24.82|:80… connected.
HTTP request sent, awaiting response…Read err (connection reset by peer) in headers.
This repeated for 20 trys and gave up.
Thanky John, I’ve just updated the post
the line:
$ wget http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz
become:
$ wget https://libwebsockets.org/git/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz
I’ve not tried if the procedure works since I don’t have the original environment installed, so please if you run it let me know.
Gg