0
function notify() {
    var json = {
        'sessionId': "#{session.id}",
        'cartToken': "#{socketTokenBean.token}"
    };

    notifyAll(JSON.stringify(json)); // Push "json" to a WebSocket channel.
}
<p:inputText value="#{bean.value}" .../>
<p:commandButton value="Submit" oncomplete="notify()" process="@this" update="..." .../>

There could be another component like <p:selectOneMenu> in place of <p:inputText>. Consider it as an input component holding a quantity value in a shopping basket. bean is a session scoped managed bean holding a list of items in a shopping basket.

The oncomplete handler of <p:commandButton> invokes a JavaScript function which pushes a JSON message to a WebSocket channel. Obviously, if nothing is changed in <p:inputText>, pushing a JSON message to a WebSocket channel is worth nothing - it will unnecessarily send associated clients a notification taking unnecessary processing power.

Is there a way to invoke the notify() function here oncomplete="notify()", only if the value held by the given <p:inputText> has been changed?

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Wouldn't you rather do it server side with JSF `ValueChangeListener`? – BalusC Mar 05 '16 at 08:43
  • But how? It would not prevent the JavaScript function `notify()` from being invoked which when invoked pushes a JSON message to a WebSocket channel which in turn notifies associated clients quite unnecessarily. – Tiny Mar 05 '16 at 08:53
  • 1
    If the form is submitted, value change listeners will run (you can programmatically add them if necessary) and in action/prerender you could determine if anything is changed and then trigger the push just from server side on. This way there's no need for `notifyAll()` (I have a deja vu on this function name, perhaps you've previously asked a simliar question?) – BalusC Mar 05 '16 at 09:03
  • @BalusC: http://stackoverflow.com/questions/32426674/notify-only-specific-users-through-websockets-when-something-is-modified-in-t – Kukeltje Mar 05 '16 at 16:26
  • @Kukeltje: no this one http://stackoverflow.com/q/28793083 – BalusC Mar 06 '16 at 10:18
  • @BalusC: that was the one I was indeed looking for, but my oh my, I could not find it (::shame::) – Kukeltje Mar 06 '16 at 22:24

0 Answers0