👷MQTT (WIP)
Last updated
Last updated
MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. Designed for constrained environments, it is ideal for machine-to-machine (M2M) or Internet of Things (IoT) scenarios. MQTT operates over TCP/IP and aims to be simple and efficient, ensuring high performance and reliability even on low-bandwidth networks.
In MQTT, topics are used to filter and direct messages between clients. A topic is a string, typically representing a hierarchy, that defines the destination of the message. Topics are case-sensitive and use a forward slash (/
) as a delimiter to create levels of hierarchy. For example, home/kitchen/temperature
and home/livingroom/humidity
are distinct topics.
The create
function creates an MQTT instance with which a client connection to an MQTT broker over an arbitrary transport method (TCP, TLS, WebSocket, ecc) can be created. The MQTT client automatically handles the following:
Regular server pings
QoS flow
Automatic reconnections
Start publishing before being connected
Simply drag the create
function onto the board, name your instance and trigger.
Drag the delete
function onto the board, insert the name of the instance to be deleted and trigger it.
With the connect function, you can connect the MQTT instance to a broker.
To create the connection, drag the connect function onto the board. Insert url: {url}
into the url field, where you replace the curly brackets and their content with your URL. The following protocols are supported: mqtt
, mqtts
, tcp
, tls
, ws
, wss
, wxs
, alis
.
Then, in the second input field, add options: {}
as the main key and insert the following parameters in object format in the brackets as needed.
username
String
The username required by your broker, if any.
password
String
The password required by your broker, if any.
wsOptions
Object
{}
Specific options for WebSockets. Have a look at: https://github.com/websockets/ws/blob/master/doc/ws.md.
keepalive
Integer
60
Keep alive interval, set to 0 to disable.
reschedulePings
Boolean
true
Reschedule ping messages after sending packets.
protocolId
String
MQTT
Protocol ID
protocolVersion
Integer
4
Protocol Version
clean
Boolean
true
Set to false to receive QoS 1 and 2 messages while offline.
reconnectPeriod
Integer
1000
Interval between two reconnections in milliseconds. Disable auto reconnect by setting to 0.
connectTimeout
Integer
30000
Time to wait before a CONNACK is received in milliseconds.
queueQoSZero
Boolean
true
If connection is broken, queue outgoing QoS zero messages.
autoUseTopicAlias
Boolean
false
Enabling automatic Topic Alias assign functionality.
resubscribe
Boolean
true
If connection is broken and reconnects, subscribed topics are automatically subscribed again.
With the publish function, you can send a message to a topic. Insert the topic in the first box, the message in the second one and lastly, decide on the options to publish with.
The options need to be constructed as an object, with the highest key being options
with the following three parameters nested inside, as needed:
qos
Integer
0
Quality of Service level
retain
Boolean
false
Whether to retain the message or not. For an explanation of retained messages, see here.
dup
Boolean
false
Marks the message as duplicate.
The publishJson
function works in the same way as the publish function, but delivers a JSON message instead. Therefore, in the second input field, you should insert a JSON object.
With the subscribe
function, you can subscribe to one or multiple topics. If you only want to subscribe to one topic or with a wildcard, insert the topic in the first input box and the qos setting in the second one. If you want to subscribe to multiple topics, ignore the options box and insert an object into the topic input box in the following scheme:
MQTT supports two types of wildcards for subscribing to multiple topics:
Single-level wildcard (+
): Matches exactly one topic level. For instance, home/+/temperature
matches home/kitchen/temperature
and home/livingroom/temperature
.
Multi-level wildcard (#
): Matches all subsequent levels. For example, home/#
matches home/kitchen/temperature
, home/livingroom/humidity
, and any other topic starting with home/
.
Using these wildcards, you can flexibly subscribe to a range of topics.
The onJsonMessage
function needs to be triggered once after the app is started to start listening to events. It then converts the received events into JSON objects.
The onStringMessage
function needs to be triggered once after the app is started to start listening to events. It then converts the received events into strings.
The onBinaryMessage
function needs to be triggered once after the app is started to start listening to events. It leaves the received events in binary format.
With the unsubscribe function, you can remove a subscription by inserting the topic name in the input field and triggering.
With end
, you can disconnect from the MQTT broker. The disconnect may take a second, but can be forced to immediately happen, without waiting for the inflight messages to be handled, by inserting forced: true
in the input field.
isConnected
simply returns true
if a connection to an MQTT broker is open and false
if not.
With isReconnecting
you can check if the client is currently trying to reconnect to the MQTT broker. A reconnection in progress is indicated by true
.
You can output the message ID of the last message sent by the client with getLastMessageId
.