Realtime data with Mosquitto, Apache and mod_websocket_mosquitto

Things are getting better and better ๐Ÿ˜‰

This is a follow-up on my previous post about real-time data delivery to web pages (or anything else, in fact). The comments and emails I got on that post were surprising; MQTT is really hot and so is the push-mechanism that’s being used. Willem was one of them. I know Willem for some years now and we share some common interests; the first being Domotica and we’re also both tinkering with the Simplecortex, our recently installed smart meters and we both use Mosquitto as MQTT broker.

A week ago Willem developed an Apache module, which made life even more easier than it already was – with his Apache module, the only thing you need to make a real-time webpage is the mosquitto javascript that can be downloaded fromย here – nothing more! Less components, less chance that something will collapse… ๐Ÿ˜‰

So, this time the components to build a real-time webpage are:

  • Mosquitto;
  • Apache;
  • mod_websocket_mosquitto from Willem;
  • Mosquitto javascript/websockets client;
  • a simple webpage.

It all started when Willem sent me an email with hisย mod_websocket_mosquitto source code attached and some instructions on how to use it. I have Apache running on my proxy server (for hosting this weblog) but since I don’t have any development tools installed on that machine (VM, to be precise) I booted a Fedora 17 VM on my PC (with VirtualBox, cause Windows 7 Virtual PC didn’t like to boot Fedora 17), installed scons and compiled the source code. The resulting module was copied to the proxy server including a configuration file for defining some configuration details:

<IfModule mod_websocket.c>
 Loadmodule mod_websocket_mosquitto modules/
 <Location /mosquitto>
 MosBroker proxyserver.hekkers.lan
 MosPort 1883
 SetHandler websocket-handler
 WebSocketHandler modules/ mosquitto_init

But somehow it didn’t work… I went back to my Fedora17 VM, installed the Apache module on it, but the result stayed the same – no go. I couldn’t find any trace of connection attempts being made to the Mosquitto broker, I got no error messages, nothing; looked like a dead end…

But then I got the tip to disable SELinux. And that was it, phew! Why didn’t I think of that, I’ve had troubles with SELinux in the past (some 7-8 years ago) but never again since, so the thought to disable it just didn’t occur to me… grr! Hurry back to the proxy server: Bingo!

[fake@proxyserver sbin]# /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
mosquitto version 1.0.4 (build date 2012-10-27 16:56:56+0200) starting
Opening ipv6 listen socket on port 1883.
Opening ipv4 listen socket on port 1883.
New connection from
New client connected from as mosqpub/3624-giga.

But the thought of a broker being accessible from the Internet gave me the creeps; with not much effort, anybody who knows a bit about how all this works, could easily start publishing to my broker. But that’s not going to happen, I need some way to make sure my ‘production‘ broker is not affected in any way.

So for that I went back to my Fedora17 VM, downloaded the mosquitto broker source code, built it and installed it on my proxy server. This mosquitto instance on the proxy server would be the one that the web page (“the Internet”) would connect to. And to feed this proxy-broker with information from my production broker, I defined a 1-way bridge from production to proxy, which can be done from the mosquitto.conf file, like this:

connection domotica
address domotica.hekkers.lan
topic /# in

This way the proxy server knows about all the topics on the domotica (production) server, but topics published on the proxy server won’t arrive at the domotica server. After a restart of mosquitto I saw this:

[fake@proxyserver sbin]# /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
mosquitto version 1.0.4 (build date 2012-10-27 16:56:56+0200) starting
Opening ipv6 listen socket on port 1883.
Opening ipv4 listen socket on port 1883.
Connecting bridge domotica
New connection from
New client connected from as mosqpub/3728-giga.

Again, wow…I added a web page to my Domotica website, so that y’all can watch my Smart meter data in real-time as well. Enjoy!

PS.: Use Firefox or Chrome, those are tested and work.

Tagged , . Bookmark the permalink.

9 Responses to Realtime data with Mosquitto, Apache and mod_websocket_mosquitto

  1. Roger says:

    That’s very neat. Could I make a suggestion without having seen the internals of mod_websocket_mosquitto (and without being able to find anything else on it)?

    I’m guessing what Willem has done is simply forward the MQTT data coming over websockets to the broker defined by the MosBroker value. This is great – exactly what was envisaged for mosquitto.js usage. However, I think restricting the module to MQTT does it a disservice. It could be used to *anything* over websockets. I’d urge Willem to change it to mod_websockets_transport or similar to allow this mode because it’d be even more useful. The mod_websockets for lighttpd that I use on does this – the config allows you to choose the websockets url to match against a websockets subprotocol, and a host/port to forward the data to/from. It’s great.

    I’d also point you to which has some thoughts on keeping Websocket MQTT implementations compatible. mosquitto.js will be making the minor changes to match that in version 1.1.

  2. Willem says:

    Roger I dropped you an email …

  3. disaster_ita says:

    Hi Robert, thank you to share with us your work.

    I’m very interested to your remote sensor with xbee and your reasoning on battery life, sleep, interrupt.

    I’m a developer, but with few electronic groundwork. I’m starting with Arduino and XBee right now.
    My idea is a DIY security system alarm with remote XBee for sensors.

    It is possible to contact you to share some opinion?
    You have my email address if you want.

    Thank you

    PS: sorry for posting comment here, but can’t comment the articles of interest (year 2010/11)

  4. kacy says:

    Just to say thank you for this (well hidded) article, I’ve managed to make it working ๐Ÿ˜‰
    By the way, selinux can be kept if you activate http_can_network_connect ๐Ÿ˜€
    [ user@host ] setsebool -P httpd_can_network_connect 1

  5. Fra says:

    Hi Robert
    I am working with an arduino yun based system that uses a remote mqtt broker to send info and receive commands for the actuators.
    i saw your excellent work and

    I think is really what we need to make our project more reliable and secure.
    Nevertheless I am still struggling to understand.
    I compiled and installed it as per instructions.
    I still don’t grasp the mosq.html file, my ignorance on websockets?

    do I need a /var/www/mysite/mosquitto directory?

    do I need a ws:// with a javascript file in it?

    do I have to compile the contents of the dir examples and put it into /var/www/mysite/mosquitto dir?

    Thank you for some directions or documentation

    I think this module is what I have been looking for since a long time.

    regards, Fra

  6. Fra says:

    I have solved!
    I was missing definition in my apache2.conf

    now it works perfectly, thanks to Boris for his analysys.

    Thank you for your Post Robert. It’s Great.

Leave a Reply

Your email address will not be published. Required fields are marked *