Search code examples
jsfprimefaceswebsocketgrowl

Update p:growl from @ApplicationScoped bean listening on p:socket


I have application scoped bean that is listening to web-socket. When message is received I would like to update growl. But something like below doesn't work because it is not in request / response time. It is possible to do this?

RequestContext.getCurrentInstance().update("growl");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "value"));

Solution

  • What you need to do is send a PrimeFaces push event from the bean to all attached clients on a general websocket shared by all clients. In that event you send a message like in the PrimeFaces push notify example

     public void send() {
        EventBus eventBus = EventBusFactory.getDefault().eventBus();
        eventBus.publish(CHANNEL, new FacesMessage(StringEscapeUtils.escapeHtml(summary), StringEscapeUtils.escapeHtml(detail)));
    }
    

    And then in the page show that message

    <p:growl widgetVar="growl" showDetail="true" />
    <p:socket onMessage="handleMessage" channel="/notify" />
    
    <script type="text/javascript">
        function handleMessage(facesmessage) {
            facesmessage.severity = 'info';
    
            PF('growl').show([facesmessage]);
        }
    </script>