Skip to main content
Version: 8.1

Implement asynchronous message exchange

Level: intermediate
Example

Implement asynchronous exchange among the modules.

  1. Set the configuration object as a parameter of the handler function in the module that publishes the message.

  2. Add a callback function to the configuration object.

    Example that publishes messages and retrieves the result
    ...
    this.sandbox.publish("AsyncMessageResult",
    /* Configuration object specified as a handler function parameter. */
    {
    /* Callback function. */
    callback: function(result) {
    this.Terrasoft.showInformation(result);
    },
    /* Scope of the callback function execution. */
    scope: this
    });
    ...
  3. Return asynchronous result in the handler method of the subscriber module the module subscribes to a message. Use the callback function parameter of the published message.

    Example that subscribes to a message
    ...
    this.sandbox.subscribe("AsyncMessageResult",
    /* Message handler function. */
    function(config) {
    /* Handle the incoming parameter. */
    var config = config || {};
    var callback = config.callback;
    var scope = config.scope || this;
    /* Prepare the resulting message. */
    var result = "Message from callback function";
    /* Execute the callback function. */
    if (callback) {
    callback.call(scope, result);
    }
    },
    /* Execution scope of the message handler function. */
    this);
    ...