Skip to main content
Version: 8.1

Implement message exchange between modules

Level: intermediate
Example

Create the UsrSomeModule module. Implement the following messages in the module:

  • MessageToSubscribe address message that has Subscribe direction
  • MessageToPublish broadcast message that has Publish direction

Subscribe to the MessageToSubscribe message sent by another module. Cancel the message registration.

1. Create a module

  1. Open the Configuration section and select a user-made package to add the schema.

  2. Click AddModule on the section list toolbar.

  3. Fill out the schema properties in the Module Designer.

    • Set Code to "UsrSomeModule."
    • Set Title to "SomeModule."

    Click Apply to apply the changes.

  4. Add the source code in the Module Designer.

    UsrSomeModule
    /* Declare a module called UsrSomeModule. The module has no dependencies. 
    Therefore, an empty array is passed as the second module parameter. */
    define("UsrSomeModule", [], function() {
    Ext.define("Terrasoft.configuration.UsrSomeModule", {
    alternateClassName: "Terrasoft.UsrSomeModule",
    extend: "Terrasoft.BaseModule",
    Ext: null,
    sandbox: null,
    Terrasoft: null,

    init: function() {
    this.callParent(arguments);
    },
    destroy: function() {
    this.callParent(arguments);
    }
    });
    return Terrasoft.UsrSomeModule;
    });
  5. Click Save on the Module Designer’s toolbar.

2. Register a message

  1. Declare message configuration objects in the messages schema property.
  2. Add to the init() method the sandbox.registerMessages() method call that registers messages.
Register a module message
...
/* Collection of the configuration message objects. */
messages: {
"MessageToSubscribe": {
mode: Terrasoft.MessageMode.PTP,
direction: Terrasoft.MessageDirectionType.SUBSCRIBE
},
"MessageToPublish": {
mode: Terrasoft.MessageMode.BROADCAST,
direction: Terrasoft.MessageDirectionType.PUBLISH
}
},
...
init: function() {
this.callParent(arguments);
/* Register a message collection. */
this.sandbox.registerMessages(this.messages);
},
...

3. Publish a message

  1. Implement the processMessages() method in the module schema.
  2. In the processMessages() method, call the sandbox.publish() method that publishes the MessageToPublish message.
  3. Add the processMessages() method call to the init() method.
Publish a module message
...
init: function() {
...
this.processMessages();
},
...
processMessages: function() {
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
...

4. Subscribe to a message

  1. Add the sandbox.subscribe() method call to the processMessages() method. The sandbox.subscribe() method subscribes to the MessageToSubscribe message sent by another module.
  2. Specify the onMessageSubscribe() handler method in the method parameters and add it to the module source code.
Subscribe to a message from another module
...
processMessages: function() {
this.sandbox.subscribe("MessageToSubscribe", this.onMessageSubscribe, this, ["resultTag"]);
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
onMessageSubscribe: function(args) {
console.log("'MessageToSubscribe' received");
/* Modify the parameter. */
args.arg1 = 15;
args.arg2 = "new arg2";
/* Return the result. */
return args;
},
...

5. Cancel the message registration

Cancel the message registration
...
destroy: function() {
if (this.messages) {
var messages = this.Terrasoft.keys(this.messages);
/* Cancel the message array registration. */
this.sandbox.unRegisterMessages(messages);
}
this.callParent(arguments);
}
...

Source code

UsrSomeModule
/* Declare a module called UsrSomeModule. The module has no dependencies. 
Therefore, an empty array is passed as the second module parameter. */
define("UsrSomeModule", [], function() {
Ext.define("Terrasoft.configuration.UsrSomeModule", {
alternateClassName: "Terrasoft.UsrSomeModule",
extend: "Terrasoft.BaseModule",
Ext: null,
sandbox: null,
Terrasoft: null,

/* Collection of the configuration message objects. */
messages: {
"MessageToSubscribe": {
mode: Terrasoft.MessageMode.PTP,
direction: Terrasoft.MessageDirectionType.SUBSCRIBE
},
"MessageToPublish": {
mode: Terrasoft.MessageMode.BROADCAST,
direction: Terrasoft.MessageDirectionType.PUBLISH
}
},
init: function() {
this.callParent(arguments);
/* Register a message collection. */
this.sandbox.registerMessages(this.messages);
this.processMessages();
},
processMessages: function() {
this.sandbox.subscribe("MessageToSubscribe", this.onMessageSubscribe, this, ["resultTag"]);
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
onMessageSubscribe: function(args) {
console.log("'MessageToSubscribe' received");
/* Modify the parameter. */
args.arg1 = 15;
args.arg2 = "new arg2";
/* Return the result. */
return args;
},
destroy: function() {
if (this.messages) {
var messages = this.Terrasoft.keys(this.messages);
/* Cancel the message array registration. */
this.sandbox.unRegisterMessages(messages);
}
this.callParent(arguments);
}
});
return Terrasoft.UsrSomeModule;
});