
I had been assigned the task of introducing Reverse-Ajax (aka Comet) for our Liferay-based portal. I searched a lot on Google but couldn't even find a semi-decent tutorial addressing whether Liferay will work with other Third Party Reverse Ajax Libraries like Cometd or Atmosphere.
With the help of an excellent Blog by Lucas Jellema, I was able to easily integrate Liferay and Cometd. The blog is at http://technology.amis.nl/2012/01/push-based-synchronized-slideshow-demo-application-implemented-using-cometd-and-jquery-running-on-tomcat/
The Chief benefit of this approach, as opposed to the DIY approach shown in the Liferay Pages, is that you don't have to reinvent the wheel.
We will create two separate Liferay Projects, one for hosting the Cometd-Server and the second one for subscribing and publishing via Jsp/Javascripts.
To create the Cometd-Server
1. Create a New Liferay Project in Eclipse: cometdTestServer
2. Import the following Libraries into the /WEB-INF/lib folder:
bayeux-api-2.4.0.jar
cometd-java-client-2.4.0.RC3.jar
cometd-java-common-2.4.0.jar
cometd-java-server-2.4.0.jar
javax.inject-1.jar
jetty-client-7.6.0.RC5.jar
jetty-continuation-7.6.0.RC5.jar
jetty-http-7.6.0.RC5.jar
jetty-io-7.6.0.RC5.jar
jetty-jmx-7.6.0.RC5.jar
jetty-servlets-7.6.0.RC5.jar
jetty-util-7.6.0.RC5.jar
jetty-websocket-7.6.0.RC5.jar
slf4j-api-1.6.4.jar
slf4j-simple-1.6.4.jar
3. Create folder com.cometd.server in 'docroot/WEB-iNF/src' and create these two files inside: LiferayBayeuxInitializer.java and LiferayCometdService.java
4. In web.xml create the following entries:
<servlet>
<servlet-name>cmtd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>cmtd</servlet-name>
<url-pattern>/cmtd/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>liferayBayeauxInitializer</servlet-name>
<servlet-class>com.cometd.service.LiferayBayeuxInitializer</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
5. Technically, the Cometd Server is ready now. All we need is another portlet which will subscribe to the channel we created in Step 3 "/liferay/newUser" along with a listener and then on calling the 'publish methodâ the listener code will be executed.
6. But I have also provided a LiferayClient that can test our Cometd Server as a stand-alone.
7. Only alter the value of default URL from 'default
URL = "http://localhost:8080/CometdServer-portlet/cmtd' to your context path.
8. Just bear in mind that on initialization, we subscribe to the liferayCometdListener whose onMessage() method is invoked to all listening clients when the publish() method is called. This is the key to understanding the behavior.
9. Deploy the portlet - no need to add it to your portal - and then run the client application.
10. You should see a command promt message 'Enter three lines â¦'. Please note you may miss this message because many initialization and connection status messages will be streaming on.
11. Enter input strings followed by âÂÂEnterâ button thrice, upon which the client.publish(data) will be issued which will cause the liferayCometdListener to be fired and publish your three inputs.
12. The listener being fired implies that your Cometd was configured properly and that Cometd is working on Liferay.
All Code has been uploaded at
http://code.google.com/p/cometd-with-liferay/downloads/list
Part II Coming Soon ....