Background on Comet #
Comet, or long polling, is a mechanism that allows server-to-client communication. It is designed to avoid the performance and scalability shortcomings of standard polling. However, comet implementations can vary greatly. In Liferay 6.1 an API was introduced to help write portable comet code.
There are 2 major components to the system. First is the Liferay integration with the Comet implementation of choice. Currently Liferay 6.1 has integration with the Tomcat Comet implementation. This integration handles connection management and delivery/sending of data from/to a user. This component communicates with the business logic via the CometHandler interface.
For every comet connection, a new CometHandler instance is cloned from a prototype and initialized with a CometSession object. The CometHandler will then receive data from the comet connection and eventually be destroyed, either by the client, Liferay, or by the business logic.
Along with a CometHandler, the business logic is given a CometSession object which wraps a CometRequest/CometResponse pair. These two classes function in much the same way as the ServletRequest and ServletResponse objects in the standard servlet spec. CometRequest includes various data that identifies the connection. The CometResponse is used to deliver data to the user.
Using the Tomcat CometProcessor #
Enabling the Tomcat Comet implementation conists of 2 steps:
- Configuring the Tomcat NIO connector. The Tomcat CometProcessor must use the NIO connector. Otherwise performance and scalability issues could arise. See the Tomcat documentation here
- Adding the entries to the Liferay webapp web.xml:
<servlet> <servlet-name>Comet Poller Servlet</servlet-name> <servlet-class>com.liferay.support.tomcat.longpoll.comet.CatalinaCometProcessor</servlet-class> <init-param> <param-name>cometHandlerImpl</param-name> <param-value>com.liferay.portal.poller.PollerCometHandler</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Comet Poller Servlet</servlet-name> <url-pattern>/poller/*</url-pattern> </servlet-mapping>
Note the cometHandlerImpl parameter. This parameter is used to tell the CatalinaCometProcessor what CometHandler implementation is to be used for this CometProcessor. The PollerCometHandler was written to be functionally similar to the PollerServlet. The only liferay feature that currently uses Poller is the Chat portlet. While the chat portlet can use comet, the engineering team is still working on other improvements to allow it to take advantage of comet.