Thursday, September 6, 2012

PrimeFaces 3.4 came out three days ago. Beside the usual awesomeness of new and updated components it also includes the new PrimeFaces Push framework. Based on Atmosphere this is providing easy push mechanisms to your applications. Here is how to configure and run it on latest GlassFish 3.1.2.2.

Preparations

As usual you should have some Java, Maven and GlassFish installed. If you need it out of one hand give NetBeans 7.2 a try. It is the latest and greatest and comes with all the things you need for this example. Install the parts or the whole to a location of your choice and start with creating a new GlassFish domain:

asadmin create-domain pf_push

accept the default values and start your domain

asadmin start-domain pf_push

Now you have to enable Comet support for your domain. Do this either by using the http://<host>:4848/ admin ui or with the following command:

asadmin set server-config.network-config.protocols.protocol.http-1.http.comet-support-enabled="true"

That is all you have to do to configure your domain.

The Maven Project Setup
Now switch to your IDE and create a new Maven based Java EE 6 project. Add the primefaces repository to the <repositories> section and add the primefaces dependency to your project <dependencies> section or your project's pom.xml:

It is using Log4j and if you need to have some more output it is a good idea to also include the corresponding configuration or bridge it to JUL with slf4j. To do the later, simply include the following to your pom.xml:

<p:socket /> is the PrimeFaces component that handles the connection between the server and the
browser. It does it by defining a communication channel and a callback to handle the broadcasts. The contained <p:ajax /> component listens to the message event and updates the counter field in the form. This however requires and additional server round-trip. You could also shortcut this by using a little java-script and binding the onMessage attribute to it to update the output field: