User IdleMonitor

PrimeFaces 0.9.0 introduced a new component idleMonitor to watch user actions and execute callbacks in case they go idle or active again. Simplest use would be as follows; suppose you want to display a modal dialog when the user goes idle.

<p:idleMonitor onidle="idleDialog.show();" onactive="idleDialog.hide();"/> 
	 	 	 
<p:dialog header="What's happening?" widgetVar="idleDialog" modal="true" 
	 fixedCenter="true" close="false" width="400px" visible="true"> 
	 <h:outputText value="Dude, are you there?" /> 
</p:dialog>

By default idleMonitor waits for 5 minutes so after this period in case user doesn’t do
anything, dialog will popup.

Ajax IdleEvent
Most likely, you would want to execute a callback on a JSF bean to do something useful like
invalidating the session. For this use case idleMonitor is equipped with idleListener. With a
simple methodexpression your idleListeners will be notified by passing an IdleEvent.

<p:idleMonitor idleListener="#{idleMonitorController.handleIdle}"/> 
public void handleIdle(IdleEvent event) { 
	 //Invalidate user
} 

Ajax Update
IdleMonitor is also powered by PrimeFaces Partial Page Rendering infrastructure so it can update
the page after an idleListener is invoked, this is useful to notify the user what happened while they
were away.

<p:idleMonitor idleListener="#{idleMonitorController.handleIdle}" 
	 	 update="message"/> 
	 	 	 	 	 
<h:outputText id="message" value="#{idleMonitorController.msg}" /> 
public void idleListener(IdleEvent event) { 	
	//invalidate session 
	msg = "Message from server: Your session is closed"; 
} 

Online Demo
To see idleMonitor in action check out the online demo.

One Response to User IdleMonitor

  1. matthiaswessendorf says:

    neat

%d bloggers like this: