device-mqtt, a library to perform RPC using MQTT
Project: device-mqtt, a library to perform RPC using MQTT (https://github.com/peppesilletti/device-mqtt)
Tech Stack: NodeJS, Tape, Coffeescript, MQTT
During my time working at Viriciti, I had the opportunity to work on a challenging and complex project: a system used to update the apps running on hundreds of devices mounted on electrical buses, all around the world.
The system’s architecture consisted of two main components, a client application running on the devices and a web app, used to dispatch actions to devices and overview their status. Please visit this link if you’d like to know more about the system’s design and implementation.
One of the challenges of this system was to handle the case when a device is offline and there is a new update for its apps. MQTT’s feature of message retaining was a perfect candidate to solve this problem. When a publisher sends a new message on a specific topic, all the subscribers to that same topic will receive the message. If they are offline, they will receive it once they come back online. This means that we could send updating (and not only) actions to all the devices and be sure that eventually all of the (offline and online) devices would have been updated correctly.
It sounds great so far, but here it is the hidden challenge: when sending any kind of action from the server to the device component I had no way of knowing if the action was successful. What if the device receives the action when is offline and executes it? How do we get to know its result?
That’s when I thought to extend MQTT to have a RPC-like mechanism and created the device-mqtt library. Using device-mqtt it’s possible to send an action to a specific device or to multiple devices, and use an event-listener to listen for the action’s result!
Here it is a graph showing how device-mqtt works:
The web app dispatches an action using device-mqtt, that under the hood is just an MQTT message sent to the topic commands/deviceId/randomToken, with some payload if needed. The device-mqtt server part, also subscribes to the topic commands/deviceId/randomToken/response, where it will receive the response from the device (immediately or any time in the future, cool right?).
On the other hand, the device component of device-mqtt is subscribed to the topic commands/deviceId/*, where it listens for any incoming actions.
Do you have any questions about this library? Drop me a message at email@example.com