Bridging REST to MQTT – Web apps talking to constrained devices

If you are into connecting your embedded devices to the Internet, you have most likely heard (and probably used) of the Message Queueing Telemetry Transport protocol, mostly known as MQTT,  which recently became also an OASIS standard. There are quite a few client implementations of MQTT for embedded devices:

MQTT is quite handy especially in those cases where you want to send data to your device (e.g., control commands) without having the device to continuously poll the server for updates or by implementing a server on the device (e.g., a web server) and accessing it remotely (which is a bad idea, both for security reasons, but also due to firewall-nat restrictions). However, your browser does not ‘talk’ MQTT, while in many cases you would like to a) send data from a browser and deliver them to device(s) through MQTT and b) receive data in the browser from devices that communicate over MQTT.

Fortunately there are ways to overcome this issue, by using a MQTT-to-REST bridge. To put it simply, you need a server that translates MQTT to WebSockets. You can write the server script on your own, or use an existing implementation, such as the Eclipse Ponte.

Ponte is an open source framework based on Node.JS that allows you to publish and receive the data using any protocol: HTTP, MQTT and CoAP. You can mix and match: submit with CoAP and subscribe via MQTT.

For example, you can do (assuming you have installed Ponte locally):

$ curl -X PUT -d 'world' http://localhost:3000/resources/hello

do send data over HTTP to the topic “hello:, and to receive through MQTT, you subscribe to the same topic:

$ mosquitto_sub -t "hello" -v

Additionally, you can use a simple Node.JS script (if you simply need a MQTT to WS bridge) that implements the following:

  • connects to a MQTT broker and subscribes to a topic
  • implements a WebSocket server
  • when there is data received over MQTT it sends the data to the connected WebSocket clients
  • when there is data from a WebSocket client, it dispatches the data through MQTT to a specific topic

 

You can find such a script here.

Simply edit and add the server endpoints, install the dependencies and you are ready to go!

Further reading:

Overview of Pub/Sub protocols

MQTT vs HTTPs performance on mobiles

2 thoughts on “Bridging REST to MQTT – Web apps talking to constrained devices

    • VSCP could be an alternative, since it is an application protocol that can run on top of MQTT/HTTP. However, I find it much simpler to use Paho or a Node.JS script like the one I am providing.

Leave a Reply

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

*