HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT

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:

1 listener 80
2 protocol websockets
3 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.

ws://yourhost/ws, or  ws://yourhost:80/foobar would work just fine too – Mosquitto doesn’t care about the path at all!

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 athttps://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:

1 <IfModule mod_websocket.c>
2 Loadmodule mod_websocket_mosquitto /usr/lib/apache2/modules/mod_websocket_mosquitto.so
3  <Location /mosquitto>
4  MosBroker localhost
5  MosPort 1883
6  SetHandler websocket-handler
7  WebSocketHandler /usr/lib/apache2/modules/mod_websocket_mosquitto.so mosquitto_init
8  </Location>
9 </IfModule>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值