title: Best way to use Websocket with Spring Boot and Vuejs
body: <p>I try to use Websocket with spring boot backend (as an API) and Vuejs frontend.</p>
  
  <p>I take a simple use case to expose my question. Some users are logged on my website, and there is a messaging feature. User A send a message to User B. User B is actually logged, and I want to notify User B that a new message is arrived.</p>
  
  <p>I see 3 ways to do it with websockets :</p>
  
  <p>1 - When User A send message, an Axios post is call to the API for saving message, and, if the Axios response is success, I call something like </p>
  
  <pre><code>this.stompClient.send("/app/foo", JSON.stringify(bar), {})
  </code></pre>
  
  <p>2 - When User A send message, I only call something like </p>
  
  <pre><code>this.stompClient.send("/app/foo", JSON.stringify(bar), {})
  </code></pre>
  
  <p>and it's my controller's method (annotated with @MessageMapping("/xxxx") @SendTo("/topic/yyyy")) that call facade, service, dao to first, save message, then return message to subscribers</p>
  
  <p>3 - I keep my actuals controllers, facade, services and DAO, and juste add when save is successfull something like :</p>
  
  <pre><code>@Autowired SimpMessagingTemplate webSocket; 
  ...
  @GetMapping("/send-message")
  public ResponseEntity sendMessage(@AuthenticationPrincipal User user, ....) {
      service.saveMessage(....);
      webSocket.convertAndSend("/ws/message-from", message);
  </code></pre>
  
  <p>without a new controller contains @MessageMapping("/xxxx") @SendTo("/topic/yyyy"). User B is just subscibed to "/ws/message-from"</p>
  
  <p>Could you help me.</p>
  
  <p>In the 3 way there is a good method ?</p>
  
  <p>Thanks you.</p>