You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

MessageSubscriber

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 callback attribute.

The following properties can be configured for the subscriber:

Name Type Description
clients Array of MessageClient A list of all related clients that will be subscribed on.
topic String The topic that will be subscribed
callback 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.

Key Description
topic The topic the message was sent in.
message The message’s content.

Search results for "MessageSubscriber"

MessageClient

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

Name Parameters Description
sendMessage String topic, String message Publishes a message on given topic
subscribeTopic String topic Subscribes the given topic. Invoked automatically by MessageSubscribers.
unsubscribeTopic String topic Unsubscribes the given topic. Invoked automatically by MessageSubscribers.

And the following properties

Name Type Description
subscribers Array of MessageSubscribers A list of all related subscribers.

Step 3: Create MQTTClient and MessageSubscriber

In our structr instance we navigate to the data section and select the type MQTTClient.

We create a new object with the following attributes (the default settings for RabbitMQ)

mainBrokerURL = 'ws://localhost:15675/ws'
username = 'guest'
password = 'guest'

Afterwards we check the isEnabled checkbox which triggers the client to connect to the server. If everything is configured properly it will blink green - otherwise it will blink red and result in an error.

Then we select the type MessageSubscriber and create a new MessageSubscriber with the following attributes:

topic = '*'
callback = "{ $.log($.topic, ': ', $.message) }"

This instructs the subscriber to listen to any topic and simply log the messages to the server log. We could also forward the messages to any global schema method - for example handleIncomingMQTTMessage like this:

{
  $.call('handleIncomingMQTTMessage', { topic: $.topic, message: $.message });
}

MessageSubscriber

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 callback attribute.

The following properties can be configured for the subscriber:

Name Type Description
clients Array of MessageClient A list of all related clients that will be subscribed on.
topic String The topic that will be subscribed
callback 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.

Key Description
topic The topic the message was sent in.
message The message’s content.

Step 2: Install RabbitMQ Web MQTT plugin

MQTTClient