I use mosquitto for a lot of projects; it’s a great MQTT broker. However, I’ve also always had to spin my own middleware to meld it into my websocket implementations. This isn’t the worst thing, as the approach allows a custom layer that can do heavy processing before being sent to clients.
The issue being…sometimes I just want the straight stream without the middleware.
To accomplish this, we can build a custom version of the Mosquitto 1.4 branch with libwebsockets that will allow us to configure Mosquitto to output on a websocket.
Fire up the VM on Google Compute Engine
There are two ways to start an instance on Compute Engine; the web panel or the command line. The command line is the easiest in my opinion. This assume you’ve already auth’ed (otherwise you may want to read up on the managing authentication and credentials)
Once we’re up and running, we can go ahead an ssh into that instance:
Grab some dependiences
First things first: grab your development tools:
Now, let’s grab other pieces we’ll need to build libwebsockets and mosquitto:
Fantastic! Now, let’s go get libwebsockets:
Building libwebsockets
Presuming everything above went a-okay, we should have all we need to build.
Note: I’ve shorted the zsh command prompts without the path name below; obviously we’re changing into a directory and in oh-my-zsh that would put the folder in the path…it just gets a little long on the cut and paste.
The cmake command above is important on CentOS 7; if we don’t give it the lib suffix, the build will fail.
Building Mosquitto 1.4
First, let’s pull the code from the repo using mercurial:
Okay, so you have some code. Now we need to tell Mosquitto to use libwebsockets by editing the config.mk file and enabling WITH_WEBSOCKETS:
After we’ve done that, we can build mosquitto:
Why “make binary”? Because mosquitto really isn’t very setup to build kindly on CentOS; we would have to change a lot of hardcoded paths to the docbook xsl dependencies. There is a bug marked WONTFIX that explains this (see issue 1269967). Trust me, it’s not worth the time. Just use make binary.
It all went well…or so I thought
So everything builds, you install everything, you go to fire up mosquitto and it fails. Turns out, we need to symlink our libwebsockets lib:
Well, and it would be helpful to have a very basic mosquitto.conf:
Note the “protocol websockets” line. This is going to enable our implementation.
Now, we fire mosquitto up:
Open up some ports
It would help if we told the instance to allow some traffic:
Let us not forget our websocket:
Talk with Paho JavaScript
With the server feeling chatty, you can now connect and talk with Eclipse Paho’s JavaScript implementation: Paho JavaScript Client. The wire up is fairly straightforward in the most basic setup:
Where is the docker image?
I haven’t built a docker image that implements this build. It’s on my list.
I’m not a fan of Cent, can I do this in Ubuntu?
Sure. The steps are roughly the same, but for a specific Mint 17/Ubuntu 14.04 version, see Jeremy Gooch’s post.
Why all this?
Primarily for use with my Polymer polymer-glass-timer-mqtt tag and my GDK MQTT Glass Timer application. Together, it let’s me do some nifty demo’s.