Friends! I am going to discuss just top of the CometD here.
While creating any web application for palm devices like mobile phone, tabs, ipad etc, the main villain is network failure. It always defeats our hero (web application) by kidnaping and destroying application data (data lost) on request – response chain. For smooth functioning of any web application there should be a guaranty that if data is sent from client, it will definitely reach to server and if server dispatches a response, it will certainly reach to corresponding client. To fulfill this requirement, we need a side hero i.e. a tool to establish Network reliability.
Here comes the role of Bayeux Protocol. It is used to establish the bidirectional interactions between web clients, for example using AJAX, and the web server. It provides several specifications to establish network reliability in Web Application. One of the implementation of these specifications is CometD by DOJO technology.
“CometD is a scalable web event routing bus that allows you to write low-latency, server-side, event-driven web applications. Typical examples of such applications are stock trading applications, web chat applications, online games, and monitoring consoles. CometD makes use of an Ajax push technology pattern known as Comet, but also uses emerging web standards such as WebSocket for low latency communication.is a scalable web event routing bus that allows you to write low-latency, server-side, event-driven web applications. Typical examples of such applications are stock trading applications, web chat applications, online games, and monitoring consoles.”
OHH!!!!! What a great….. English. Today, I came to know that even I can also write such English. Don’t worry, these are not my words. These are from CometD document.
I think, I have bored you a lot. So, before you start beating me, I should move on to the steps for configuring CometD and creating a Hellow world program.
Step 1) Download library and jar files: Download cometd-2.5.0-distribution.tar.gz from http://download.cometd.org/ and go to directory “cometd-2.5.0-distribution\cometd-2.5.0\cometd-demo\target”. Here, you will get cometd-demo-2.5.0.war. Use following command to unwar it: “jar –xvf cometd-demo-2.5.0.war”. Extract the following jar files from the newly created folder.
Step 2): Create a Dynamic Web Project in Eclipse.
Step 4): Create a web.xml file as given below:
<?xml version="1.0" encoding="UTF-8"?>
In this web.xml file AnnotationCometdServlet is the built-in servlet of CometD and it will receive all the request coming from client through cometD.
MyService is a java class that acts as the service class and it will be initialized by CometD container automatically and it is singleton. All the requests, coming to AnnotationCometdServlet, are forwarded to this service class.
Step 5): Create MyService.java service class:
public class MyService
private ServerSession _session;
public void configureExecuteCommand(ConfigurableServerChannel channel)
public void handleFooMessages(ServerSession remote, ServerMessage message)
remote.deliver(_session, message.getChannel(),"I am Saying Hello World From Server", null);
Function configureExecuteCommand() with annotation like “@Configure("/service/*")” makes all the service channel available for subscribing.
Function handleFooMessages() with annotation like “@Listener("/service/foo")” declares itself as the listener or subscriber for channel “/service/foo” and all the request published on this channel from client will come to this function.
Step 6): Create handshake function on client: Before starting communication with server through cometD, it is necessary to make a handshake of client with server where both detect each other’s compatibility. This is done by dojox.cometd.init() function of cometD as given in file CometDMessagingManager.js
Step 7) Create client side publisher of request on client: We have to create the client side publisher of request which publishes requests on service channel that are subscribed on java side by functions of service classes as done in echoRpc() function of user.js
Step 8) Create client side subscriber of response from server: We have to create the client side subscriber of the channel on which server sends the response. Actually server sends response on the save channel from which it has received the request. That means the client side subscriber should be created on same channel on which the client side publisher is created. It is done on echoRpc() function of user.js. In the attached application, the function echoRPCReturn() of user.js is the corresponding client side subscriber.
Step 9) Provide comet url: Provide the cometD url as http://localhost:8080/CometDIdea/cometd in the dojox.cometd.init() function in setUpMessaging() function of CometDMessagingManager.js file. This is the url where every request will be dispatched and they will be handled by AnnotationCometDServlet.java file as configured in web.xml file.
I have attached the complete application code. Please go through it for getting clear idea. Following url may be helpful.