title: Notify only specific user(s) through WebSockets, when something is modified in the database
body: <p>In order to notify all users through WebSockets, when something is modified in selected JPA entities, I use the following basic approach.</p>
  
  <pre><code>@ServerEndpoint("/Push")
  public class Push {
  
      private static final Set&lt;Session&gt; sessions = new LinkedHashSet&lt;Session&gt;();
  
      @OnOpen
      public void onOpen(Session session) {
          sessions.add(session);
      }
  
      @OnClose
      public void onClose(Session session) {
          sessions.remove(session);
      }
  
      private static JsonObject createJsonMessage(String message) {
          return JsonProvider.provider().createObjectBuilder().add("jsonMessage", message).build();
      }
  
      public static void sendAll(String text) {
          synchronized (sessions) {
              String message = createJsonMessage(text).toString();
  
              for (Session session : sessions) {
                  if (session.isOpen()) {
                      session.getAsyncRemote().sendText(message);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>When a selected JPA entity is modified, an appropriate CDI event is raised which is to be observed by the following CDI observer.</p>
  
  <pre><code>@Typed
  public final class EventObserver {
  
      private EventObserver() {}
  
      public void onEntityChange(@Observes EntityChangeEvent event) {
          Push.sendAll("updateModel");
      }
  }
  </code></pre>
  
  <p>The observer/consumer invokes the static method <code>Push#sendAll()</code> defined in the WebSockets endpoint which sends a JSON message as a notification to all the associated users/connections.</p>
  
  <p>The logic inside the <code>sendAll()</code> method needs to be modified somehow, when only selected users are to be notified.</p>
  
  <ul>
  <li>Notify only the user who is responsible for modifying the entity in question (it may be an admin user or a registered user who can modify something only after their successful login).</li>
  <li>Notify only specific user/s (not all). "Specific" means for example, when a post is voted up on this site, only the post owner is notified (the post may be voted up by any other user with sufficient privileges).</li>
  </ul>
  
  <p>When an initial handshake is establised, <code>HttpSession</code> can be accessed as stated in <a href="https://stackoverflow.com/a/23405830/1391249">this</a> answer but it is still insufficient to accomplish the tasks as mentioned above by two bullets. Since it is available when the first handshake request is made, any attribute set to that session afterwards will not be available in the server endpoint i.e. in other words, any session attribute set after a handshake is established will not be available.</p>
  
  <p>What is the most acceptable/canonical way to notify only selected users as mentioned above? Some conditional statement(s) in the <code>sendAll()</code> method or something else somewhere is required. It appears that it has to do something other than only the user's <code>HttpSession</code>.</p>
  
  <p>I use GlassFish Server 4.1 / Java EE 7.</p>