Sparkplug host application is a type of component in the Sparkplug system. The host application connects to the MQTT broker, and consumes real-time Sparkplug messages sent to it from other Sparkplug components - edge nodes and their devices. It can also (optionally) send commands to edge nodes and/or devices.
Rapid Toolkit for Sparkplug takes care of maintaining the proper message sequences, and cooperation between components in the Sparkplug system.
In Rapid Toolkit for Sparkplug, to develop your own Sparkplug host applications, you make use of the consumer object.
In Rapid Toolkit for Sparkplug, the consumer object is an instance of the EasySparkplugConsumer Class. You can create one or more instances of this object, and call methods on it/them. Together, the operations you make on these objects will appear as coming from a single Sparkplug host application (you can also use Rapid Toolkit for Sparkplug to write applications that act as multiple Sparkplug host applications in a single program, but that is rarely needed).
See Sparkplug Consumer Object Operations And Notifications for information how your code can subscribe to and publish Sparkplug data using the consumer object.
The design of the consumer object is such that an identification of the MQTT broker to connect to is passed into every operation. This way, applications that connect to more then one MQTT broker can easily do some without having to create a separate consumer object based on the broker URL. The MQTT broker URL, together with other related, is stored in a host descriptor, represented by an instance of the SparkplugHostDescriptor Class.
On the other hand, with this design, writing an application that connects to just one MQTT broker is also straightforward - simply pass the same broker connection data (usually just the URL) to all method calls on the consumer object.
If, for some reason, you do not want to pass the same broker connection data (more precisely, host descriptor) to every method call, you can configure an implicit host decsriptor on the consumer object, by setting it in the ImplicitHostDescriptor Property. Method calls on the consumer object can then leave the host descriptor in the method argumenst unfilled, which means that it will have the default value of SparkplugHostDescriptor.Null (note that this is not a null reference), and the consumer object will use the implicit host descriptor instead.
The following example illustrates the use of implicit host descriptor.
In Sparkplug, host application are required to report their online and offline status in the MQTT topics structure. For this purpose, each Sparkplug host application must a unique host application ID, so that its status can be distinguished from the others in the MQTT topic structure. The status of the Sparkplug host application accessible by its host application ID, can then be used by other components in the Sparkplug systems, e.g. by edge nodes configured to use this application as their primary host application, and only only publish their data when the primary host application is online.
Unless instructed otherwise, Rapid Toolkit for Sparkplug auto-generates a host ID that is conformant with the Sparkplug syntactic rules, and it also attempts to make it unique within the Sparkplug system.
If you want you program to behave as a Sparkplug host application with specific ID, you can do so by setting the HostId Property in the host descriptor passed to the methods on the consumer object. Just make sure to pass the same host descriptor all such method calls.
The following example illustares how to specify the Sparkplug host application ID.
Normally, Rapid Toolkit for Sparkplug takes care of starting and stopping the Sparkplug application. The Sparkplug host application is started when the first subscription is made, and stopped after all subscriptions are removed. While started, the Sparkplug host application attempts to stay connected to the Sparkplug system (MQTT broker).
In some scenarios, you may want to control the starting and stopping of the host Sparkplug host application yourself, but without a need to unsubscribe from everything in order to disconnect the application from the MQTT broker. To achieve this, first obtain the host application object (an instance of the EasySparkplugHostApplication Class) using the FindOrCreateHostApplication Method, and set its AutoStartStop Property to false. You can then use the Start Method and Stop Method to start and stop the Sparkplug host application explicitly.
Note that when the Sparkplug host application is disconnected from the MQTT broker, the subscriptions that you have made on the consumer object receive a data notification with error indication (see Data Notifications in Sparkplug Consumer and Rapid Toolkit for Sparkplug Consumer Error Model); also, publishing Sparkplug commands will not be possible in this state and will result in an error (reported by an exception thrown from synchronous publishing methods).
The following example illustrates how to start and stop the host application.
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.