I was just asked how one can deploy a similar setup as the iot.eclipse.org MQTT sandbox, where MQTT over WebSockets is available on port 80, just like the rest of the website.
There are actually two ways of achieving this.
Mosquitto as the main frontend
It’s a little-known fact but together with built-in WebSockets support (added in version 1.4), Mosquitto also can act as basic HTTP server, and directly serve a bunch of static resources for you. The config option you’re looking for is “http_dir
“, that will allow you to serve the content of a directory over HTTP.
Granted you are running a version of Mosquitto that has WebSockets support, here how your mosquitto.conf file should look like to enable WebSockets *and* regular HTTP connections:
listener 80 protocol websockets http_dir /home/johndoe/htdocs
Of course, you will need to make sure that you do not have any other daemons (like Apache, nginx, …) already running and using port 80 🙂
Once Mosquitto is setup this way, you can use any MQTT client that supports WebSockets to connect to ws://yourhost
URI.
[info]ws://yourhost/ws
, or ws://yourhost:80/foobar
would work just fine too – Mosquitto doesn’t care about the path at all![/info]
Apache front-end + mod_websocket_mosquitto
Since it’s likely you actually want a “real” HTTP server to serve your website (for security reasons, for being able to run PHP, etc.), another approach is to use Apache as the main HTTP front-end, as you would normally do, and configure it to tunnel WebSockets connections made on a given URI to your Mosquitto broker.
You can download an Apache module that does exactly that at https://github.com/willem4ever/mod_websocket_mosquitto. The instructions to compile and install it are pretty straightforward and you will end up with something like the following in your Apache configuration:
<IfModule mod_websocket.c> Loadmodule mod_websocket_mosquitto /usr/lib/apache2/modules/mod_websocket_mosquitto.so <Location /mosquitto> MosBroker localhost MosPort 1883 SetHandler websocket-handler WebSocketHandler /usr/lib/apache2/modules/mod_websocket_mosquitto.so mosquitto_init </Location> </IfModule>
9 replies on “How to run your web server and MQTT WebSockets broker on the same port”
I get ERR_EMPTY_RESPONSE when i try to run mosquitto as the main front end and display a html file. I am trying to host locally and listening on port 8080.
So after configuring apache2, you should be able to connect to port 80 from an external MQTT client like MQTT.fx ? This is not working on ubuntu 14.04, I did all the steps and everything went fine along the way.
I also get ERR_EMPTY_RESPONSE in Chrome. The connection is reset. There are only some TCP packets transferred, of which FIN ACK is sent quite immediately, thus terminating the connection. I have no clue why this happens… Any ideas?
I figured out the problem exists in recent mosquitto 1.4.10 … when using version 1.4.8 for example, it works as described.
See also https://github.com/eclipse/mosquitto/issues/286
hi ,
i want to use mosquitto MQTT with websockets but in my pc it’s not ,
how i can solve this problem on windows 7 ,
i am waiting your response ,
thank you
Does this also work with TLS enabled?
I am running mosquitto at port port 8883 and want to redirect from apache.
(Mosquitto and apache are running in separate docker containers in the same network.)
P.S.:
I also tried to to use a proxy in https.conf:
ProxyPreserveHost On
ProxyPass /mosquitto https://mosquitto:8883
ProxyPassReverse /mosquitto https://mosquitto:8883
so that I can access mosquitto vi my_domain/mosquitto.
Hello Jens,
If you are not using TLS for actually *authenticating* your clients, you might be better off just setting your ‘local’ mosquitto to be plain TCP on port 1883, and have the Apache daemon handle and terminate the TLS connection?
Hello Benjamin,
thanks for your fast response.
unfortunately I need the functionality of MQTT / Mosquitto for authenticating. E.g. different access right by different certificate for different topics.
[…] and mod_websocket_mosquitto available and enabled. I have been trying to follow this and this, but with little […]