MQTT (Source and Target operators)

MQTT is a publish and subscribe messaging transport protocol that is designed to push messages to clients.

In a streams flow, the MQTT operator can be of type Source or of type Target:

  • MQTT as a Source operator is a subscriber to the MQTT broker, and it consumes message topics from the broker. This operator has no input parameter, but it returns messages.

  • MQTT as a Target operator is a producer to the MQTT broker, and it writes to topics in the broker. This operator has an input parameter but no return value.

MQTT has the following components:

Broker

MQTT broker is responsible for receiving all messages, filtering them, deciding who is interested in each message, and then sending the message to all subscribed clients. The broker is the central hub through which every message must pass.

You must have your own MQTT broker. Free, open source MQTT brokers available for use.

Client

The client can be both a publisher and a subscriber of messages. It can publish a message to a broker, and it can subscribe to messages from the broker.

ClientId is an identifier of each MQTT client that connects to an MQTT broker. The clientId must be unique for each broker. The broker uses it to identify the client and the current state of the client.

Topic

A topic is a string that can have a hierarchy of levels that are separated by a slash (/). This string is the routing information for the broker. Only the MQTT as Source operator can make subscriptions. When you configure this operator in a streams flow, you supply the topic name and structure based on how you defined them in the MQTT broker.


Examples

Let’s say that you defined the following topics in the broker:

  • Myhome/first_floor/bedroom/temp_detector

  • Myhome/first_floor/kitchen/temp_detector

  • Myhome/first_floor/kitchen/smoke_detector

The following examples show how to subscribe to different topics:

Subscription to a specific topic

To subscribe to the topic for kitchen temperature data, the MQTT as Source operator would subscribe to Myhome/first_floor/kitchen/temp_detector.

Subscription to a single level +

Use a wildcard to subscribe to all topics in the same topic level.

The topic Myhome/first_floor/+/temp_detector subscribes to the following topics:

  • Myhome/first_floor/kitchen/temp_detector

  • Myhome/first_floor/bedroom/temp_detector

Subscription to multi-level #

If you need to subscribe to an arbitrary number of topic levels, use a multi-level wildcard (#). You can subscribe to all underlying hierarchy levels. The # must be the last character in the topic and it must be preceded by a slash (/).

The topic Myhome/first_floor/# subscribes to all topics that begin with Myhome/first_floor:

  • Myhome/first_floor/bedroom/temp_detector

  • Myhome/first_floor/kitchen/temp_detector

  • Myhome/first_floor/kitchen/smoke_detector


Secure communication

Communication between client and broker can be secured by using the following types of certificates:

  • SSL (Secure Sockets Layer) certificate

  • Signed CA (Certificate Authority) certificate

When you create an MQTT connection in a streams flow, you can supply an SSL certificate and a Signed CA certificate. Be sure to copy and paste the certificate exactly as it appears. No white spaces or unexpected characters must be present, and alignment must be exact. The certificates of the client and broker must match, or you get a message that you cannot connect to the broker.