Inter Gadget Communication with PubSub

        In my previous blog, I wrote about Gadget/Widget and how we can use it in Liferay. Liferay provide great support of Gadgets. Liferay has out of box features like share portlet as widget and OpenSocial Gadget Editor where you can write gadget XML and publish in your portal. Liferay also provide OpenSocial Gadget plugin where admin can add third party gadgets in Liferay portal like portlets.

Inter Gadget Communication with PubSub:

        Like Inter Portlet Communication(IPC), communication between two gadgets or between gadget and portlet is also very important. OpenSocial provide mechanism to establish communication between gadget to gadget or gadget to portlet or vice versa using pubsub.

        Pubsub works like event fire/listen in AUI. There is one publisher gadget which publishes to the topic and There may be multiple consumer gadgets which has subscribed the topic. Publishers don’t send messages to subscribers directly because publisher have no knowledge about subscribers. Publisher broadcast the messages over a channel(topic). Subscriber need to subscribe channels(topics) to receive message.

Publisher Gadget

        To publish a message publisher gadget need to add topic details in Require tag like :

        Here com.example.opensocial.testgadget is topic name which subscribers need to subscribe to receive message. And publishing can be done by the Javascript method call :

        Here first parameter is topic name and second is a variable which need to publish.

Subscriber Gadget

        To receive a message from publisher, receiver gadget need to subscribe topic. Following the example to subscribe a topic:

        Here first parameter in subscribe function is topic name which need to subscribe and the second parameter is call back function name that will be called when a message is published by publisher gadget. The published message can be used in callback function:

 

        You can find publisher / subscriber gadget example on @dejuknow's github.