MQTT4Snap ! is a Snap ! library for using MQTT in Snap ! (on the cloud) and Snap4Arduino (on the edge).
MQTT is an open and secure publish-subscribe protocol for the Internet of Things, suitable for industrial applications, that can be used for many purposes.
There are several free online MQTT servers for rapid testing purpose, such as test.mosquitto.org or www.emqx.io/mqtt/public-mqtt5-broker. Never use these servers for production. One way to start is using Mosquitto on your own server, which is a popular open-source option available for Linux, Windows and Mac, fast and easy to install. For accessing directly to a MQTT broker from a browser, WebSockets support must be activated on the broker. To monitor de broker activity, MQTT Explorer is an excellent free tool.
Snap ! can be considered a no/low code programming language.
You can execute the Hello World! code as a starting point. If you use secured pages you will need the first option, else, for non secured pages, you will need the second one (without SSL):
Try at: Hello World!
Try at: Hello World! (without SSL)
Usually, the default brokers configuration only works with http pages (no https). Using http you can connect to a default local Mosquitto imstallation at localhost.
If you just want to import the MQTT4Snap ! blocks library, copy the following URL to the clipboard, and then paste it when importing into Snap !:
https://raw.githubusercontent.com/pixavier/mqtt4snap/master/mqtt-standalone.xml
Here you can see the blocks and a minimum example:
WebSocket protocol (ws:// or wss://) can be specified in the broker field. If not specified, if the Snap ! webpage is http:// type, then ws:// is used, and if it is https:// type then wss:// is used.
We can combine Snap ! programs with a remote joystick via MQTT.
The pub and sub blocks are suitable for PubSub architecture based implementations.
The request and response blocks are suitable for an asynchronous client-server architectural approach on a PubSub based infrastructure.
The request block is suitable for a synchronous client-server architectural approach on a PubSub based infrastructure.
PubSub architectures can be considered as a generalization of Client-server architectures:
- An asynchronoys request-response can be converted into synchronous with a response waiting semaphore based mechanism.
- Synchronous request-response can be emulated in PubSub with a one-time subscription (one-to-one) and a waiting semaphore.
- PubSub can be emulated in Client-server if all agents can play both client and server roles.
Waiting semaphores can be improved adding timeout management.
Synchronous and asynchronous client-server can be represented in UML, regarding the arrowheads and the activation boxes:
The disconnect block accepts the value all as a parameter, and then it closes all existing connections to the MQTT brokers.
Note: This library allways work with MQTT Quality of Service (QoS) level 0.
MQTT4Snap ! is based on the MQTT.js library, and loads it on the fly using the "load JS from url". The "load MQTT library" block sets the version used from the UNPKG CDN repo.
This project wouldn't exist without:
- The Snap ! Programming Language Project. Thanks to the University of California, Berkeley, and the SAP company for this great free online Snap ! course.
- MQTT.js
- The "load JS from url" and "load MQTT library" blocks from Bernat Romagosa
- The mqttWildcard matching function, adapted from [https://github.com/hobbyquaker/mqtt-wildcard]
MQTT4Snap ! was presented in the Snap!con 2020 Conference.