Wednesday, April 9, 2014

Shindig communication

 

There are different ways a gadget can interact with the container and vice versa.
  1. RPC
  2. Gadget to server communication
  3. Publish / Subscribe

 RPC

The RPC mechanism is used to perform Gadget to Container communication & Gadget to Gadget communication.

The following needs to be done in order to call an RPC service from a gadget:
Container Page:
§  Create a new javascript Function

§  Register the function as a javascript service using gadgets.rpc.register API.

                Gadget Page:
§  From the gadget, call the RPC service using gadgets.rpc.call API

The first parameter “” indicates that we are going to call arpc service of the parent container.
Define the callback method for processSearchTerm.



1.2.    Gadget to Server communication.


Gadget.io API provides a mechanism by which the gadget will be able to communicate with the server side.

Gadgets.io.makeRequest accepts 3 parameters.
The first param is the URI that defines a service, second one defines the callback method, and the third one is the input parameters to the service.  The content type mentioned in the parameter is given as “TEXT”, which means the service will return a TEXT. The other content types supported are DOM, FEED & JSON. This has been explained in detail here: http://code.google.com/apis/gadgets/docs/remote-content.html#Content_Types
In the above example, the uri - rest/searchResults is nothing but a REST based service defined in Wink.

The code is given below:
           



1.3.    Publish/Subscribe.

This works on the basic bulletin board concept. A gadget can publish a message and the other gadgets which has subscribed for it can obtain the information and perform some business logic.
The example given below is taken from Shindig and is part of the distribution:
Subscriber:


The gadget can subscribe to master gadget using the subscribe API. The first parameter “random-number” defines a channel which it listens to.
The master gadget publishes a message using the following API:

Once this is called, the message is published to the channel, and the callback method is called on the subscriber.


Apache Shindig - Getting started

Shindig Environment Setup (Running the example)

Software installation

There are 2 ways of setting up the Shindig environment:
1.       Directly downloading the shindig war file
§  Download Shindig war from the url: http://shindig.apache.org/download/index.html (shindig-server-1.1-BETA5-incubating.war). Download the latest war.
§  Deploy it in the App Server.

(OR) 

if you want to build from source

2.     From source.
§  Download Apache Maven
§  Ensure JDK 6 is installed in the machine.
§  Set the path to JDK and Maven.
§  Build the war file out of the source using the following command.
mvn install -Dmaven.test.skip=true
Note: We were getting some compilation errors on the test classes hence ensure that we build using the above command.
§  The WAR file would be generated – place where it is built will be mentioned at the end of running the build.
§  Deploy the WAR in the App server.

Note: Deploying the war file:
Tomcat: deploy the war in the webapps directory, rename the war to ROOT.war and restart the server.

JBoss: deploy the war in the server/default/deploy directory, rename the war to ROOT.war and restart the server.

Invoke the following url to ensure that the application is properly installed.



Developing Gadgets

Steps:
§  Choose the gadget type – HTML or URL
§  Develop the gadget as defined in this section.
§  Place it in a location where it can be identified by an URI.
§  Develop the container code (which displays these gadgets)
§  Develop miscellaneous services like communication between container, gadget and server depending on your requirement.

Writing a simple Gadget (HTML): 




Code to display the gadget: 


The following were done in the above code:
§  Get the location of the gadget xml
§  Set the layout for the gadget container.
§  Create and add the gadget.
§  Render the gadget using the renderGadgets() API.

Writing a simple Gadget (URL): 

Just mention the url name in the attribute “href” instead of the html content.
There is no change in the way we display the URL type gadget. It follows the same step as we followed in the previous section. 

Apache shindig - Overview

Shindig is an open source framework from Apache that acts as a container for gadgets. iGoogle is built using this framework.

So what is a gadget:

Gadgets are mini applications (similar to portlet) that are written in HTML, Javascript& XML. Gadgets are iframes that are rendered into your web page. 


Date and time gadget, weather gadget, are all shindig gadgets.
These gadgets can interact with the page, communicate between gadgets.

The developer normally writes the gadget as HTML written within the gadget XML. Gadget server processes these XML and converts them into html snippets and displays them as iframe in the web page.

Types of Gadgets:
§  HTML gadgets
This is a type of gadget where the content of the gadget (html) resides within the gadget XML.

§  URL type gadgets
This is a type of gadget where the content of the gadget resides in a third party server. The URI is given as an attribute in the xml.

What is a gadget Container

It is the context where the gadget is embedded. In developer terms it is the page where the gadget resides. The page has some javascript APIs which inturn interacts with the Gadget server for rendering, communicating etc.,

What you can do using Shindig.

Shindig acts as a container for the gadgets. The gadget can perform the following using Shindig
-          Communicate with other gadgets using pub/sub feature.
-          Communicate with the container using RPC.
-          Communicate with server using gadget.io
-          Obtain open social information using the Open social API. 

How can i develop my own gadgets.

Click here to find out how to create your own gadgets.

Some reference links: