title: Websocket: How To Push A Message To A Target User
body: <p>I'm trying to implement a push notification in spring using websocket and with the use of sock.js. </p>
  
  <p>These are the code snippets:</p>
  
  <pre><code>    public class NotifyController {
  
          @MessageMapping("/notifications")
          @SendTo("/get/notifications")
          public Greeting greeting(HelloMessage message) throws Exception {
              new Greeting("Hello, " + message.getName() + "!");
          }
      }
  
  
      public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
          @Override
          public void configureMessageBroker(MessageBrokerRegistry config) {
              config.enableSimpleBroker("/get/notifications");
              config.setApplicationDestinationPrefixes("/gssocket");
          }
  
          @Override
          public void registerStompEndpoints(StompEndpointRegistry registry) {
              registry.addEndpoint("/notifications").withSockJS();
          }
      }
  </code></pre>
  
  <p>This is the code in the front..</p>
  
  <pre><code>    function connect() {
          var notificationSocket = new SockJS('/notifications');
          stompNotificationClient = Stomp.over(notificationSocket);
  
          stompNotificationClient.connect({}, function(frame) {
              console.log('Connected: ' + frame);
              stompNotificationClient.subscribe('/get/notifications', function(greeting){
                  showGreeting(JSON.parse(greeting.body).content);
              });
          });
      }
  
      function sendNotification() {
          var name = "test"
          stompNotificationClient.send("/gssocket/notifications", {}, JSON.stringify({ 'name': name }));
      }
  </code></pre>
  
  <p>I've already managed to make it work. But the question is, how can I push the message to certain target users. For example, there are 5 online users namely: user1, user2, user3, user4 and user5. I want the notification to be published to user1 and user2 only. Can you give me any idea on how to achieve this one? I'm thinking of either doing it in the backend or in the frontend. Or there is another way to achieve this using spring. </p>
  
  <p>Somebody help me please. </p>
  
  <p>Thank you.</p>