title: Spring STOMP over Websocket - "private" messaging
body: <p>I'm working on WebSocket chat. I've made simple one that sends every message to everyone. But I'm trying to make something like private messaging and I really don't know how to do that. I've tried different options but I'don't understand how to make it work.</p>
  
  <p>Here's my code:</p>
  
  <p><em>WebSocketConfig.java</em></p>
  
  <pre><code>@Configuration
  @EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry config) {
          config.enableSimpleBroker("/topic/", "/queue/", "/user/");
          config.setApplicationDestinationPrefixes("/app");
      }
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint("/gs-guide-websocket").withSockJS();
      }
  }
  </code></pre>
  
  <p><em>GreetingController.java</em></p>
  
  <pre><code>@Controller
  public class GreetingController {
  
      @MessageMapping("/hello")
      @SendTo("/topic/greetings")
      public Greeting greeting(Message message) throws Exception {
          return new Greeting(message.getFrom_user_id(), message.getMessage(), message.getTo_user_id());
  
      }
  }
  </code></pre>
  
  <p>And my js function to connect and sending message:</p>
  
  <pre><code>function connect() {
  var socket = new SockJS('/gs-guide-websocket');
  stompClient = Stomp.over(socket);
  var theUserId = 1;
  stompClient.connect({user:theUserId}, function (frame) {
      setConnected(true);
      console.log('Connected: ' + frame);
      //stompClient.subscribe('/topic/greetings', function (greeting) {
      stompClient.subscribe('/topic/greetings', function (greeting) {
          showGreeting(JSON.parse(greeting.body).content);
      });
  });
  }
  
  
  function sendName() {
  stompClient.send("/app/hello", {}, JSON.stringify(
          {
              'from_user_id': parseInt($("#from_user_id").val()), 
              'message': $("#message").val(),
              'to_user_id': parseInt($("#to_user_id").val())
          }));
  }
  </code></pre>
  
  <p>How to make private message work? Could somebody explain me how it should work?</p>
  
  <p>Cheers Andrew!</p>