Skip to content

cymplecy/mqtt4snap

 
 

Repository files navigation

MQTT4Snap !

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.

Blocks and usage

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):

(JavaScript extensions must be ticked)

Try at: Hello World!

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:

Minimal 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.

pub / sub blocks

The pub and sub blocks are suitable for PubSub architecture based implementations.

pub sub blocks

request / response blocks

The request and response blocks are suitable for an asynchronous client-server architectural approach on a PubSub based infrastructure.

request response blocks

request reporter block

The request block is suitable for a synchronous client-server architectural approach on a PubSub based infrastructure.

request reporter block

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:

Synchronous and asynchronous client-server

disconnect block

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.

MQTT.js library

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.

load MQTT library

Acknowledgents

This project wouldn't exist without:

MQTT4Snap ! was presented in the Snap!con 2020 Conference.

About

MQTT for Snap!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 56.5%
  • C++ 36.8%
  • HTML 6.7%