Monday, February 24, 2014

Dashboard - Gadget communication using pub/sub within WSO2 UES

What is pub/sub?

This is a messaging pattern where message senders named publishers and consumers named subscribers. In this architecture publishers are not directly sending messages to consumers but sending to a channel where interesting parties(subscribers) subscribe to this channel and consume messages.

In this blog-post, portal or my dashboard acts as the publisher where user triggers any event to publish and simple gadget acts as the subscriber.

My requirement is to have something similar to the following, When user triggers "Publish a random number" button, dashboard should publish a random number to a predefined channel.Subscriber gadget subscribes to this channel and print the published random number.


How can we create this type of a dashboard?

Refer WSO2-UES documentation on how to create a gadget and how to add gadgets to dashboards.

Note :- 
If you browse <UES_HOME>/repository/deployment/server/jaggeryapps/portal/dashboard-template/files/ directory this is where we store template files for your dashboards. You may update index.jag.hbs file according to your preference, but keep in mind hereafter every time you create a dashboard, these changes will reflect on the dashboard.

Since our requirement is to have this template for all my dashboards, lets update the index.jag.hbs file.

You can find the full updated source gist here[1].
Note I have added the following HTML snippet to get the user input and display the published value.
<input type="button" value="Publish a random number" onclick="publish()"/>
<div id="output"> </div>
Also I have introduced following script, in order to publish a random number to the defined channel.
<script>
function publish() {
               var message = Math.random();
               UESContainer.inlineClient.publish('my-channel', message);
               document.getElementById("output").innerHTML = "Number that is published to the channel : " + message;
        }
</script>
Lets move to the gadget, you may find the source for this sample subscriber gadget here[2] and refer "Subscriber Gadget" section from this blog post[3] if you need to build the subscriber gadget from the scratch.

Interested about inter-gadget communication within WSO2-UES? you may refer the same blog post[3] done by tanya.

[1]. https://gist.github.com/udarakr/d9ebe3c32b64ec4e1ba0
[2]. https://svn.wso2.org/repos/wso2/people/tanya/pub-sub/subscriber/
[3] http://tanyamadurapperuma.blogspot.com/2013/12/inter-gadget-communication-with-wso2-ues.html

No comments:

Post a Comment