title: how to send notification to all logged in users using websockets and SockJS
body: <p>I want to notify all logged in users that how many users are logged-in/online in my app, that was so simple I have following code to do so in configure method os WebSecurityConfigurerAdapter I have simply</p>
  
  <pre><code>@Override
      protected void configure(HttpSecurity http) throws Exception {          
          http.sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry()).expiredUrl("/");
  
      }
  </code></pre>
  
  <p>and at controller side I have</p>
  
  <pre><code>    @Autowired
      @Qualifier("sessionRegistry")
      private SessionRegistry sessionRegistry;
          @Secured("ROLE_USER")
      @RequestMapping(value = "/users/{userId}/{userName}", method = RequestMethod.GET)
      public String showUserProfile(Model model){
  List&lt;Object&gt; principals = sessionRegistry.getAllPrincipals();
  
          List&lt;String&gt; usersNamesList = new ArrayList&lt;String&gt;();
  
          for (Object principal: principals) {
              if (principal instanceof org.springframework.security.core.userdetails.User) {
                  usersNamesList.add(((org.springframework.security.core.userdetails.User) principal).getUsername());
              }
          }       
  
          model.addAttribute("onlineUsers", "Online users are " + usersNamesList.size());
  return "layout_user_profile";
  
  }
  </code></pre>
  
  <p>I get number of online users, but not real time, I want to notify all users that some new user is logged-on/logged-off of that App, I have <em>@EnableWebSocketMessageBroker</em> annotated class </p>
  
  <pre><code>@Configuration
  @EnableWebSocketMessageBroker
  public class WebSocketBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer{
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
          stompEndpointRegistry.addEndpoint("/users")
          .setHandshakeHandler(new CurrentLoggeInUserHandshakeHandler())
          .withSockJS();      
      }
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry registry) {
          registry.setApplicationDestinationPrefixes("/app")
                  .enableSimpleBroker("/topic", "/queue");
  
      }
  
      class CurrentLoggeInUserHandshakeHandler extends DefaultHandshakeHandler{
  
  
          @Override
          protected Principal determineUser(ServerHttpRequest request,
                  WebSocketHandler wsHandler, Map&lt;String, Object&gt; attributes) {
              User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
              String username = user.getUsername();
              return new UsernamePasswordAuthenticationToken(username, null);
          }   
  
      }
  
  }
  </code></pre>
  
  <p>and my client side is </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var webSocketC;
      var stompClient;
      $(document).ready(function(){
  
          webSocketC = new SockJS('/app/users');
  
          stompClient = Stomp.over(webSocketC);
  
          stompClient.connect({}, function(frame){
              stompClient.subscribe('/topic/users', function(message){
                  //TODO Here
              });
  
          }, function(error){         
                  console.log("error " +  error);         
          });     
      });
      &lt;/script&gt;
  </code></pre>
  
  <p>with this code I get browser console output as </p>
  
  <pre><code>Opening Web Socket...
  stomp.js:134 Web Socket Opened...
  stomp.js:134 &gt;&gt;&gt; CONNECT
  accept-version:1.1,1.0
  heart-beat:10000,10000
  
  &lt;&lt;&lt; CONNECTED
  version:1.1
  heart-beat:0,0
  user-name:john-doe@mail.com
  
  connected to server undefined
  stomp.js:134 &gt;&gt;&gt; SUBSCRIBE
  id:sub-0
  destination:/topic/users
  </code></pre>
  
  <h1>Conclusion</h1>
  
  <p>I can not find a way to send messages to all users, whether I use <em>@EnableWebSocketMessageBroker</em> annotated class send that notification or or the client side code is the place to get the desired trick done? and how that could be done? </p>