Subscribing to data is the most common functionality of Sparkplug host applications (consumers). Rapid Toolkit for Sparkplug allows you to make data subscriptions extremely easily, with a single method call, without even having to explicitly make connection to the Sparkplug broker, or other unnecessary boilerplate code seen in other toolkits.
For efficiency, Sparkplug allows the producers to "bundle" multiple metrics in a single message payload. Rapid Toolkit for Sparkplug allows you to work with bundled metrics, or it can unbundle them for you. This is useful in applications that treat every metrics as a data stream completely independent of others.
The data you subscribe to are delivered to your code using data notifications, in the form of events or callbacks (you can choose). See Data Notifications in Sparkplug Consumer for discussion on the data notifications, their meaning and contents. If you are using events handlers to process the data notifications, you need to add them before making the subscription, otherwise the initial data notifications might be lost.
Rapid Toolkit for Sparkplug also uses the data notifications to provide information related to the status of the MQTT connection.
When subscribing to data, you need to identify what you are subscribing to. This requires you to specify the Sparkplug groups, edge nodes, devices (optional), and metrics. In Rapid Toolkit for Sparkplug, any of these items can be specified exactly, by providing its ID or name, or with a use of filter. The filter allows you to subscribe to a wider range of data, with a single call. For example, you may easily subscribe to all metrics in the given edge node, or *any* edge node in the Sparkplug system.
Rapid Toolkit for Sparkplug uses a modified concept of MQTT topic filters (with optional wildcards) for its filters.
For Sparkplug groups IDs, edge node IDs, and device IDs, you can use a single "#" or "+" instead of a concrete ID to denote "any ID". For example, "#" in place of an edge node ID means "any edge node".
For Sparkplug metric names, you can use the full MQTT topic filter syntax to specify the metrics you are interested in. For example, "Folder1/+/MyMetric" prescribes a metric name starting with segment "Folder1" and ending with segment "MyMetric", with a single segment in the middle which can be anything. Metric name filter "+" denotes metric name with a single segment; metric name filter "#" denotes any metric name whatsoever. Note: Do not be confused by the fact that in Sparkplug, metric names do not participate in forming the MQTT topic names, and therefore MQTT filtering cannot be used for metrics. This is a functionality added by Rapid Toolkit for Sparkplug which simply reuses the familiar syntax for somewhat different purpose.
In order to subscribe to data notifications for individual Sparkplug metrics, call one of the following methods:
The subscriptions methods return an integer handle which can later be used to unsubscribe from the metric.
The following example illustrates subscribing to all metrics directly on a given edge node.
The example below illustrates subscribing to all metrics on a give device.
In order to subscribe to data notifications for incoming Sparkplug payloads, call one of the following methods:
Normally, "#" is used as a metric name filter, meaning that all metrics will be included in the data notification. There is nothing that prevents you, however, from specifying a narrower filter, if you have such a need.
The subscriptions methods return an integer handle which can later be used to unsubscribe from the payload.
By default, Rapid Toolkit for Sparkplug puts all metrics of the edge node or device in the notification, including those that have not been explicitly sent in the received data message. This behavior can be changed, see Complete and Incomplete Datasets further below in this article.
The following example illustrates subscribing to all metrics directly on a given edge node.
When you subscribe to metrics or payloads in Rapid Toolkit for Sparkplug, the subscriptions stays alive until you explicitly unsubscribe, or until the consumer object (instance of the EasySparkplugConsumer Class) is disposed of. The subscriptions also outlive possible disconnections from the MQTT broker caused e.g. by network disruptions or computer failures.
In order to unsubscribe from a metric, call the UnsubscribeMetric Method and pass it the metric subscription handle returned to you when you called a metric subscription method. You can also call the UnsubscribeAllMetrics Method to unsubscribe from all metrics at once. The metric subscription handle is no longer valid after you make the method call.
In order to unsubscribe from a payload, call the UnsubscribePayload Method and pass it the payload subscription handle returned to you when you called a payload subscription method. You can also call the UnsubscribeAllPayloads Method to unsubscribe from all payloads at once. The payload subscription handle is no longer valid after you make the method call.
In order to unsubscribe "from everything" (all metrics and payloads), use the UnsubscribeAll Method (an extension method).
The following example illustrates how to unsubscribe froma previously subscribed metric.
Sparkplug edge nodes and devices are required to specify all their metrics when they publish their Birth messages. The subsequent Data messages from the Sparkplug edge nodes and devices can contain only the metrics that have changed (we call it the incomplete dataset). If the consumer wants a complete picture of all the metrics in the edge node or device (we call it the complete dataset), it needs to put the together the last know data with the data from the newest update. Rapid Toolkit for Sparkplug does this for you. That is, when you subscribe to an edge node or device payload, the notifications that you receive always contain all the metrics. The metrics that were just received are there of course; any metrics that were not contained in the received Data message but were specified in the Birth message are filled in with last-known data.
This functionality make it easy to process the datasets in most real-world scenarios - such as e.g. performing a row-wise logging into a database or a CSV file, or performing calculations on multiple metrics based on one sampling from the device. Rapid Toolkit for Sparkplug is also perfectly suited for scenarios that do not require complete dataset - they can typically be handled by subscribing to individual (or all) metrics (as described earlier in this article), instead of by subscribing to a payload.
If you want to subscribe to payloads as they are received, and want to deal with incomplete datasets, you can set the DeliverCompleteDataSet Property in the subscription arguments to false. This will cause the subscription to deliver payload object with only the metrics that were actually contained in the received message.
The following example illustrates the code that can be used to subscribe to incomplete datasets.
Example
Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.