Polling is a way to poll a server periodically in order to trigger server-side changes or update parts of a web page. The polling technology in PrimeFaces is represented by the poll
component. It is an AJAX-ified component that has the ability to send periodical AJAX requests.
In this recipe, we will update a feed reader periodically to show current sports news. A growl
component will be updated with the same interval too in order to show the time of the last feed update.
The p:poll
component in the following code snippet invokes the showMessage()
listener method every 10 seconds and updates a feed reader and a growl. The listener method generates the current time. Furthermore, we will define a widget
variable in order to stop or start polling using the client-side API. This occurs via command buttons. Take a look at the code for this discussion:
<p:growl id="growl"/> <p:poll id="poll" listener="#{pollingBean.showMessage}" update="sportFeed growl" interval="10" widgetVar="pollWidget"/> <p:commandButton type="button" value="Stop Polling" style="margin:15px 5px 15px 0;" onclick="PF('pollWidget').stop();"/> <p:commandButton type="button" value="Start Polling" style="margin:15px 0 15px 0;" onclick="PF('pollWidget').start();"/> <h:panelGroup id="sportFeed" layout="block"> <p:feedReader value="http://rss.news.yahoo.com/rss/sports" var="feed" size="10"> <h:outputText value="#{feed.title}" style="font-weight: bold"/> <br/> <h:outputText value="#{feed.description.value}" escape="false"/> <p:separator/> </p:feedReader> </h:panelGroup>
The corresponding screenshot illustrates an update with p:poll
:
Refer to the Setting up and configuring the PrimeFaces library recipe of Chapter 1, Getting Started with PrimeFaces, to see mandatory dependencies for the feedReader
component.
The interval
attribute of p:poll
defines the time interval, in seconds, at which to execute periodic AJAX requests. The default value is 2 seconds. In the example, we set it to 10
. Similar to any other AJAX-ified components, we can specify components to be processed and updated partially with the process
and update
attributes, respectively. The update
attribute in the example contains IDs of p:feedReader
and p:growl
.
Polling can be stopped and started using the stop()
and start()
widget methods respectively. We defined two push buttons to execute pollWidget.stop()
and pollWidget.start()
on a click event.
Poll
also supports the autoStart
mode. By default, polling starts automatically on page load. To prevent this behavior, set the autoStart
attribute to false
.
Another useful setting is the timeout
attribute, which defines a timeout for AJAX requests in milliseconds. The timeout is not set by default. Setting a valid timeout allows breaking off long-running requests.
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter11/polling.jsf
.