The Xentara MQTT client v2.0
User Manual
Topics
See also
Xentara Elements in the Xentara user manual

Topics are Xentara elements that represent a single MQTT topic published by an MQTT client element. Topic elements must always be children of client elements.

Each topic element has the following properties:

  1. The topic the message should be published to, as described in chapter 3.3.2.1 Topic Name in the MQTT specification.
  2. The payload of the message, as described in chapter 3.3.3 PUBLISH Payload in the MQTT specification. The format of the payload is described in Message Payloads.
  3. The QoS to be used for the message, as described in chapter 3.3.1.2 QoS in the MQTT specification.
  4. An option specifying whether the message should be retained by the broker, as described in chapter 3.3.1.3 RETAIN in the MQTT specification. The meaning of the retain option is described below.

Retained Topics

Generally, subscribed clients will only receive messages from the broker as they are published. Newly subscribed clients will not know about any previous messages, and thus will not have access to any data until the next time it is published. Sometimes, especially when using event-based publishing, this can be undesirable. In order to mitigate this problem, MQTT supports so called retained messages. Retained messages are stored by the broker, and are sent to any new client once it subscribes to the topic. This allows new clients to receive the last message published on a particular topic.

Warning

Retained messages are retained even after the client disconnects. This can cause stale, out of date messages to be sent to newly subscribed consumers by the broker. If you are using retained messages, it is strongly recommended that you implement a state-based lifetime strategy. Otherwise, it will be impossible for consumers to determine whether the retained messages are still valid, or if they are out of date because the Xentara MQTT client is no longer connected to the broker. This may cause consumers to operate on stale data, which is undesirable.

If you are only using a single topic with retained messages, then you can alternatively use a message to discard any stale messages, as described in Using the Will to Discard Stale Messages.

Publishing Topics

MQTT topic elements have a task named publish that must be inserted into a Xentara execution pipeline in order to schedule when the topic should be published to the broker. If you do not schedule this task, then the topic will never be published.

The publish task of a topic can be inserted into a pipeline attached to an arbitrary combination of Xentara events and/or timers. Commonly, however, one of the two strategies described below are used.

Publishing a Topic Using a Timer

The simplest way to publish a topic is to attach the publish task to a timer. The topic will be published whenever the timer expires.

Optionally, the task can also be attached to the connected event of the client it belongs to, so that it will be published immediately after the connection to a broker has been established. This is especially useful if the timer has a longer period, like one or more minutes. This will ensure that the data will be available as soon as possible after the connection has been established, without having to wait for timer to expire.

Topics attached to a timer need not necessarily be retained. If the topic is not retained, then a newly subscribed consumer will not receive any messages for the topic until the next time the timer expires. This may be undesirable for timers with a long period, as it may take a long time for a new consumers to receive the first message.

Attention
If you are using retained messages, it is strongly recommended that you implement a state-based lifetime strategy. Otherwise, it will be impossible for consumers to determine whether the retained messages are still valid, or if they are out of date because the Xentara MQTT client is no longer connected to the broker. This may cause consumers to operate on stale data, which is undesirable.

Event-Based Publishing of a Topic

A topic can also be published as a response to one or more Xentara events. For topics publishing the data of one or more data points, the topic is typically published in response to the changed events of some or all of the data points in question. Alternatively, the data can be attached to the change event belonging to one or more of the attributes contained within the payload, or to any other arbitrary event.

When publishing a topic using events, it is almost always necessary to attach the publish task to the connected event of the client it belongs to, in addition to the regular events. This will ensure that the initial values of the topic will be published as soon as the connection to the broker is established. Otherwise, the values will not be available until one of the regular events occur, which might take a very long time. In case of a topic attached to a change event, for example, the topic would not be published until the value actually changes, which may only happen a long time after the connection to the broker has been established. In the case of very static data, like error states or set points, the change event may never be raised, and hence the topic not be published at all.

Likewise, it is almost always necessary to retain messages related to an event-based topic. Otherwise, a newly subscribed consumer will not receive any messages for the topic until the next time the topic is published, which may take an arbitrarily long time.

Attention

If you are using retained messages, it is strongly recommended that you implement a state-based lifetime strategy. Otherwise, it will be impossible for consumers to determine whether the retained messages are still valid, or if they are out of date because the Xentara MQTT client is no longer connected to the broker. This may cause consumers to operate on stale data, which is undesirable.

If you are only using a single topic with retained messages, then you can alternatively use a message to discard any stale messages, as described in Using the Will to Discard Stale Messages.

Accessing Topics

See also
Accessing Xentara Elements in the Xentara user manual MQTT topics have the following members:
Attributes
keyThe topic’s primary key.
nameThe topic’s name. The name is the last component of the primary key.
UUIDThe unique UUID of the topic.
typeThe topic’s element type. For MQTT topics, this is always “MQTT Topic”.
categoryThe topic’s category. For MQTT topics, this is always “transaction”.
brokerAddressThe host name or network address of the broker the topic’s client connects to.
portNumberThe port number of the broker the topic’s client connects to.
clientIDThe MQTT client identifier the topic’s client advertises to the broker on connection.
topicThe MQTT topic the data is published to.
QoS

The numeric value of the MQTT QoS (quality of service) used for the messages. MQTT defines the following three QoS values:

QoS Meaning
0 At most once
1 At least once
2 Exactly once
retainedWhether the broker is instructed to retain messages for this topic.
stateA Boolean value denoting whether the topic is being published successfully. This attribute will be true if the topic is being published successfully, or false if there is a problem.
errorThis attribute contains an error message denoting the error if the “state” attribute is false.
Events
publishedThis event is raised whenever the topic has been published successfully.
Tasks
publishPublishes the topic’s payload to the broker.