How can I create a more fine grained web socket in JSF?

48 Views Asked by At

In my JSF application I'm trying to use web sockets to push updates from backend to frontend. The backend publishes messages on an ActiveMQ topic which I'm listening to (on @ApplicationScoped level).

I've got two channels which are created by injecting on a @WebListener, like this:

@Inject
    public void setControllerEventChannel(@Push(channel = "controllerEventChannel") PushContext controllerEventChannel) {
        this.controllerEventChannel = controllerEventChannel;
    }

    @Inject
    public void setGroupEventChannel(@Push(channel = "groupEventChannel") PushContext groupEventChannel) {
        this.groupEventChannel = groupEventChannel;
    }
<o:socket channel="controllerEventChannel" onmessage="onMessage" />

<h:commandScript name="onMessage" actionListener="#{backingBean.controllerChannelCallback()}"
                             process="@this" update="@none"/>

The challenge is, I've got many controllers and many groups. How do I push messages on these channels such that all opened views don't receive all callbacks? The views are controller or group specific. My current solution is filtering events in the actionListener method and it's, in a word, terrible.

I wish I could create channels dynamically, but I don't there's a way to do that in JakartaEE?

1

There are 1 best solutions below

0
BalusC On

For this you can use the user attribute,

<o:socket channel="some" user="#{user.id}">

with

Collection<Long> recipientUserIds = recipientGroup.getUserIds();
someChannel.send(message, recipientUserIds);

Or even

<o:socket channel="some" user="#{user.group.id}">

with

someChannel.send(message, recipientGroup.getId());