How can server push asynchronous changes to a HTML page created by JSF? -
when create jsf page, client request allows generation of html dynamically using combination of java code , html. can introduce hooks in html page using jsf framework, allow server update html page based on asynchronous events occurring later @ server, via different threads?
jsf 2.3+
you can use @push
, <f:websocket>
this. below kickoff example updates data table upon application scoped event fired backend.
<h:datatable id="notifications" value="#{bean.notifications}" var="notification"> <h:column>#{notification.message}</h:column> </h:datatable> <h:form> <f:websocket channel="push"> <f:ajax event="updatenotifications" render=":notifications" /> </f:websocket> </h:form>
@named @applicationscoped public class bean { private list<notification> notifications; @inject private notificationservice service; @inject @push private pushcontext push; @postconstruct public void load() { notifications = service.list(); } public void onnewnotification(@observes notification newnotification) { notifications.add(0, newnotification); push.send("updatenotifications"); } public list<notification> getnotifications() { return notifications; } }
@stateless public class notificationservice { @inject private entitymanager entitymanager; @inject private beanmanager beanmanager; public void create(string message) { notification newnotification = new notification(); newnotification.setmessage(message); entitymanager.persist(newnotification); beanmanager.fireevent(newnotification); } public list<notification> list() { return entitymanager .createnamedquery("notification.list", notification.class) .getresultlist(); } }
jsf 2.2-
if you're not on jsf 2.3 yet, need head 3rd party jsf libraries.
- omnifaces has
<o:socket>
(jsr356 websocket + cdi) - primefaces has
<p:socket>
(atmosphere) - icefaces has icepush (long polling)
noted should <o:socket>
basis jsf 2.3 <f:websocket>
. if have found lot of similarities, that's correct.
primefaces uses atmosphere under hoods (which troublesome setup without maven). atmosphere supports websockets fallback sse , long polling. icefaces based on ancient long polling technique. of not implement native jsr356 websocket api later introduced in java ee 7.
omnifaces uses native jsr356 websocket api (supported in java ee 7 servers , tomcat 7.0.27+). therefore simple setup , use (one jar, 1 context param, 1 tag , 1 annotation). requires cdi (not hard install on tomcat), enables push non-jsf artifact on (e.g. @webservlet
). security , jsf view state keeping reasons, supports one-way push (server client), not other way round. can keep using jsf ajax usual way. jsf 2.3 <f:websocket>
largely based on omnifaces <o:socket>
, hence you'll find lot of similarities in apis (jsf - omnifaces).
alternatively, can use polling instead of pushing. pretty every ajax aware jsf component library has <xxx:poll>
component, such primefaces <p:poll>
. allows send exery x seconds ajax request server , update content whenever necessary. it's less efficient push.
Comments
Post a Comment