The Structr Messaging-Engine module provides a generic abstraction of a publish and subscription model based on topics.
The basic idea is that a MessageClient acts as a Connector to a broker or an abstraction of such and provides the ablity to send messages and to (un)subscribe to topics. Its counterpart is the MessageSubscriber, which relates to MessageClients and is triggered when a message for the subscribed topic is received by the client. During this process the subscriber evaluates a script supplied as one of its attributes and has access to the message’s topic and content in that context.
This allows for flexible and powerful interaction with Structr’s core functionality.
Apart from the basic abstraction of a MessageClient, the module provides different implementations that act as connectors to their respective technology.
As such the module provides a MQTTClient and a KafkaClient as spezialized implementations.
Details on how to setup these entities are found in their respective documentation.
MessageClient is the base abstraction of a client connecting to a broker in the Messaging-Engine Module module.
The basic capabilities of the client include the following methods
|String topic, String message||Publishes a message on given topic|
|String topic||Subscribes the given topic. Invoked automatically by MessageSubscribers.|
|String topic||Unsubscribes the given topic. Invoked automatically by MessageSubscribers.|
And the following properties
|Array of MessageSubscribers||A list of all related subscribers.|
KafkaClient is a specialization of the MessageClient included in the Messaging-Engine Module module.
The KafkaClient connects to a Kafka broker and handles the publication as well as subscription of messages.
KafkaClient extends MessageClient and provides the following additional properties.
|Array of String|| List of kafka brokers to connect to. Format: |
Note: The consumer used in the implementation expects String-based message contents.
MQTTClient is a specialization of the MessageClient included in the Messaging-Engine Module module.
The MQTTClient connects to a MQTT broker and handles the publication as well as subscription of messages.
MQTTClient extends MessageClient and provides the following additional properties.
|String|| Protocol used for the MQTT connections. Default: |
|String||The URL for the MQTT connection|
|Integer||The port to connect to|
|Integer||Quality of Service Range: 0~2|
|Boolean||A boolean value that indicates whether this MQTTClient is enabled (should connect to the broker)|
|Boolean||A boolean value that indicates whether this MQTTClient is connected to the broker|
MessageSubscriber is a generic abstraction of a subscriber included in the Messaging-Engine Module module.
It can attach to any kind of MessageClient and will subscribe on that client with the specified topic.
If the client receives a message on given topic, a callback will be made to the subscriber and the message will be evaluated in a scripting context that is specified in the subscribers
The following properties can be configured for the subscriber:
|Array of MessageClient||A list of all related clients that will be subscribed on.|
|String||The topic that will be subscribed|
|String||Specifies a script that is evaluated upon the reception of a relevant message.|
Note: Instead of using a specific topic, it is also possible to use
* as a wildcard subscription.
The default scripting context is StructrScript and the message’s content is injected as well and available via retrieve() under the following attributes.
|The topic the message was sent in.|
|The message’s content.|