A simple MQTT to Websocket Bridge using Mosquitto and pywebsocket
23 Aug 2010UPDATE: I have now isolated the problems to 64 bit linux, the setup described here does work without using the standalone web server and using apache…just not on my main system!
Following on from my experiences installing and testing pywebsocket I now move to the main reason why I bothered…to create a bridge to allow a user to view a web page showing a live stream of MQTT messages.
It should be noted that despite making the effort to setup pywebocket with Apache this time I will be using the standalone web/websocket server provided with pywebsocket (standalone.py) as I’m still trying to debug why this code doesn’t work with apache!
Server Side Code
On the server it is a simple matter of writing a websocket handler file in python to subscribe to an mqtt topic then stream the messages through the web socket. Here I use the python MQTT client library included with mosquitto.
I defined the callbacks required by mosquitto then pynotify, web_socket_transfer_data(request)
being where all the cool stuff happens, this function is called on connection to the web socket and then loops waiting for MQTT messages.
To use this code save it in your websocket handler folder was a file ending in _wsh.py then access it using the filename without the ending.
As already mentioned this code doesn’t work when using pywebsocket with apache - to launch the included standalone webserver launch standalone.py with the following command (when in pywebsocket/src/mod_pywebsocket/):
so in my case I use
Front End
I used the same html file as detailed in my previous post, it’s simply a matter of browsing to the appropriate url not forgetting that it’s on port 8080 (or whatever port you define) to avoid conflicting with apache.
To send test MQTT messages I used the Java Client included in the ia92 package provided by IBM as pictured.
If anyone knows why this doesn’t work with apache I would appreciate them telling me! At the moment I suspect it’s some privileges thing but I may be wrong