1. MQTT
A summary of MQTT
and how to use it in Reactive Blocks
Publish/Subscribe via
in Reactive Blocks
2. MQTT: Overview
■ MQTT is a protocol on top of TCP/IP to
send messages via a broker, using a
publish/subscribe pattern.
■ OASIS standard since November 2014
■ More info on http://mqtt.org
■ Alternatives to MQTT include HTTP,
AMQP and CoAP
■ MQTT is typically used to send event data
from gateways to a backend, and
commands from a backend to a gateway
By default, MQTT uses port 1883 or port 8883
(secured). Make sure that traffic via these
ports is not blocked by your firewall.
Ports
With MQTT, clients can be behind a NAT
without problem. The clients initiate an
outgoing TCP connection over which the
server may reply.
NAT
!
3. A subscriber connects to a broker and
subscribes to one or more topics. Once a
publisher sends a message to a topic, all
subscribers that subscribed to that topic
are notified and will receive the
message.
Several publishers may send to the
same topics. Several subscribers may
subscribe to the same topics.
Publish/Subscribe Pattern
publisher broker
subscribe (topic)
subscriber
publish (topic, payload)
message (topic, payload)
4. Topic Structures
■ Each message is published with a specific topic. A subscriber listens to one or
more topics. The broker forwards a message to all subscribers with a
matching topic
■ There is no predefined topic structure in MQTT. Publisher and subscriber
simply need to agree on a topic structure.
■ Topics can be structured into different levels, separated by a “/“. Chosen
wisely, the topic structure can help the receiver to filter messages it wants to
receive.
■ The number sign “#” matches any number of levels. The plus sign “+” matches
exactly one level.
4
5. Topic Example
■
5
By using topics systematically, receivers may filter which messages they want to
receive. The following topic structure is an example:
iot/type/<device-type>/id/<device-id>/event/<event-id>/format/<format-ID>/
(The segments in brackets are replaced by specific strings for each message.)
A receive may select to receive all events sent by gateways of type “raspberry” by
subscribing to the following topic:
iot/type/raspberry/id/+/event/+/format/+/
A receiver may select to receive all “temperature” events sent in json format, to
any device of any device type:
iot/type/+/id/+/event/temperature/format/json
6. March 2012 - Business Confidential - Bitreactive AS
How to get started
MQTT in Reactive Blocks
7. Install Reactive Blocks
■ In Eclipse 4.4 or Eclipse 4.5, open the Marketplace
■ Search For Reactive Blocks
■ Also install the Feature Reactive Blocks for OSGi
■ For detailed installation instructions, visit http://www.bitreactive.com/installation/
7
8. How to Get This Library
■ After installing Reactive Blocks
■ In Eclipse, select File / Import / Reactive Blocks
/ Import Libraries, Projects and Building Blocks
■ Select the library MQTT Feature
8
In the following, we describe how to use the
OSGi version of our building block library for
MQTT.
OSGi!
9. MQTT Client
Configures and manages the MQTT connection. This block is
required. Messages are only sent and received while it is
active.
Building Blocks for MQTT
Subscribe
Subscribes to a topic (or list of topics) and receives the
messages sent to that topic. To subscribe to more than one
topic, use several instances of this block.
Publish
Send messages to a certain topic. You can use more than one
instance of this block.
10. March 2012 - Business Confidential - Bitreactive AS
Advanced Features
11. Quality of Service
■ 0… at most once: Delivery is not guaranteed, no retry. For streaming data
where one individual event is not important.
■ 1… at least once: Delivery is guaranteed, but the message can be duplicated.
■ 2… exactly once: Message is sent exactly once, but at with a higher overhead.
■ By default, QoS of a message is 0. To change it, set the QoS flag in the message
before you send it.
11
Messages can be sent with a certain quality-of-service, which depends how much
effort the protocol spends to deliver it.
12. Retained Messages
■ A sender may mark a message as “retained”.
■ A retained message is kept on the server.
■ As soon as a client subscribes to a topic that matches the retained message, it
will receive the retained message.
12
publisher broker
subscribe (topic)
subscriber
publish (topic, payload)
message (topic, payload)
«retained»
Note that the
subscriber subscribes
after the message was
sent.
■ To retain a message, mark it as retained with the API before sending it.
13. Automatic Reconnect: The MQTT
subsystem maintains the connection and
reconnects in case the connection goes
down. Reconnect works either
immediately or after a timeout that
increases with each failed connection
attempt. Messages with qos 0 are
discarded while the connection is down.
Persistence: Messages with qos>0 are
buffered until they are sent. By default, the
buffer is in memory only. To use a file
buffer, set the flag and choose the file
name in the parameters object.
Time-to-live: Messages can have a time-
to-live value, after which they are
discarded. By default, messages do not
time out.
Security: By default, MQTT uses TCP as
protocol and the payload is sent in clear
text. To use TSL, set the flag
useSecureConn() in the parameters object.
If the broker requires a username and
password, use the corresponding methods
in the parameters object.
MQTT Features
When using the building blocks, the following features are available. You can
configure them with the parameters objects passed to the MQTT Client block.
14. Multiple Connections
■ You can simultaneously maintain several MQTT connections, either to the same or
towards different MQTT brokers.
■ The separate connections can be configured individually (persistence, ttl,
security)
■ Each connection is identified with a multiton-Id, set as instance parameter on
each block.
■ Blocks with the same multiton-Id belong to the same connection.
14