1

I am trying to implement a real time notification system (eg. when an admin edits an user, that specific user only should be notified). I managed to implement the Primefaces example . Now i need it to notify only specific users.

I have changed the NotifyResource class to the following:

@PushEndpoint("/notify/{id})

public class NotifyResource {

@PathParam(value = "id")
private String id;

@OnMessage(encoders = { JSONEncoder.class })
public FacesMessage onMessage(FacesMessage message) {
    return message;
}

}

(sorry for the formatting)

The websocket in the .xhtml page where the notification should be shown is the following:

<p:growl id="msj" widgetVar="wmsj" showDetail="true" />

    <p:socket onMessage="handleMessage" channel="/notify/#{userManagementBean.notifyId}">
        <p:ajax event="message" update=":frm" />
    </p:socket>

    <script type="text/javascript">
        function handleMessage(facesmessage) {
            facesmessage.severity = 'info';

            PF('wmsj').show([ facesmessage ]);
        }
    </script>

Below is the method form the backing bean(UserManagementBean) that pushes the notification:

public void pushNotification(){
    String summary = "Notification:";
    String detail = "You were edited!";
    String CHANNEL = "/notify/" + getNotifyId();

    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish(CHANNEL, new FacesMessage(summary, detail));
}

The variable notifyId accessed usging getNotifyId() is set everytime a user is edited(it's the user's id, not a very good chosen name, but I'll change that). The backing bean is annotated with @ManagedBean and @ViewScoped, both from the javax.faces.bean package.

The pushNotification() method is called from another .xhtml page with the following:

<h:form>                       
        <p:commandButton value="Send notification!" actionListener="#{userManagementBean.pushNotification()}" />
</h:form>

After doing this and debugging, I realised that the method onMessage() from the NotifyResource class is never reached in this case. I am not really sure how the @PathParam works in combination with @PushEndpoint. I mention that the URL has the user's id in it: */welcome.xhtml?id=1 or whatever the user's id is.

0 Answers0