How to Stop Polling From Server Side

PrimeFaces Poll component enables doing periodic requests in a specified interval, at some point you may need to stop the polling depending on a change on server side. With PrimeFaces 2.2.1 this can be implemented using RequestContext API as;

<p:poll listener="#{bean.listener}" update="sth" oncomplete="handlePollComplete(xhr, status, args)" widgetVar="mypoll" />
function handlePollComplete(xhr, status, args) {
    if(args.stopPolling) {
        mypoll.stop();
    }
}
public void listener() {
    if(condition) {
        RequestContext.getCurrentInstance().addCallbackParam("stopPolling", true);
    }
}

A bit of work right? Definitely against “make it easy” aim of PrimeFaces, so as of 3.0.M2 we can now do;

<p:poll listener="#{bean.listener}" update="sth"  stop="#{bean.stop}"/>
private boolean stop = false;

public void listener() {
    if(condition) {
        stop = true;    
    }
}

And that’s it, PrimeFaces will handle stopping the poll under the hood.

3 Responses to How to Stop Polling From Server Side

  1. Daniel says:

    Nice!!!!

    gonna give it a try

    Thx!

  2. jmrunge says:

    Thanks man!!! Great feature!

  3. Sereyboth says:

    Very great post! Thank jmrunge !

%d bloggers like this: