Unknown macro: {next_previous_links}
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Follow the instructions below to configure a gadget with pub-sub for inter-gadget communication:

Step 1 - Publish the data in the publisher

Define the gadget publishing channel in the gadget.json file, to define the gadget as a publisher gadget. For more information on how to add a publishing channel to a gadget, see Creating a Gadget

Use the following code format, which defines a global method for publishing, to publish the data using the publisher channel:

Format
gadgets.Hub.publish(publisher_channel_name, message)
  • publisher_channel_name - Name of the channel defined as the notifier in the gadget.json file.
  • message - Data to publish through the channel.

Example:

Example
var message = "Publisher channel"; 
gadgets.Hub.publish('publisher', message);
  • publisher_channel_name -  this Publisher gadget the publishing channel is named as "publisher". Therefore, the data publishing takes place under the channel name "publisher". You can use the above code block in the respective event to publish data.
  • message - This refers to the publishing data that can be of any data type.

Step 2 - Configure a pub-sub link between the publisher and subscriber

In the above section, we looked at how to configure the pub-sub link between publisher and subscriber. Now, lets look at how to use the data coming through the publisher channel in the subscriber gadget for other functions.
Use the following code format, which defines a global method for subscribing, to get subscribed data via the subscriber channel:

Format
gadgets.Hub.subscribe(listener_name, function (topic, message, subscriber_data)    {
       //put your code here
})
  • listener_name - Define the name of the listener channel, which is defined in the gadget.json file that corresponds to the subscriber gadget, as the first parameter being passed. 

Thereafter, define a callback function that receives three parameters to get notifications on the published data, which is being published by the publisher gadget.

It is not mandatory to define all three parameters in the callback function. Instead, only define the required parameters in the order, which is defined in the above format, based on the information that you want to receive from the publisher.

  • topic -  This defines the topic that corresponds to the publisher channel (Notifier channel).
  • message - This contains the data published by a publisher gadget.
  • subscriber_data - This defines the subscriber channel (Listener channel).

Example:

The following example defines a subscriber gadget that has a listener channel named "subscriber". Therefore, the data subscribing takes place under the channel name "subscriber".

Example
gadgets.HubSettings.onConnect = function() {
    gadgets.Hub.subscribe('subscriber', function(topic, data, subscriberData) {
        // Rest of the call back code goes here
    });
};

The gadgets.HubSettings.onConnect() callback function is called when the connection between publisher and subscriber channel is established.

  • No labels