title: How to send a message through web socket to a connected user?
body: <p>I want to send a message through a web socket to a specific user. So far I can open a web socket and read message from client like that:</p>
  
  <pre><code>@ServerEndpoint(value = "/wsep")
  public class WebSocketEndpoint {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEndpoint.class);
  
      private Session session;
  
      @OnOpen
      public void onOpen(Session session) {
          this.session = session;
          try {
              session.getBasicRemote().sendText("You are connected. Your ID is " + session.getId());
          } catch (Exception e) {
              LOGGER.error("Error on open web socket", e);
          }
      }
  
      @OnMessage
      public void onClientMessage(String message, Session session) {      
          LOGGER.info("Message from {} is: {}", session.getId(), message);
      }
  
      @OnClose
      public void onClose(Session session) {
          this.session = null;
          LOGGER.info("{} disconnected", session.getId());
      }
  }
  </code></pre>
  
  <p>I have an independent service which creates message in destination to a user. My <code>Message</code> class is a simple POJO:</p>
  
  <pre><code>public class Message {
      private String fromUserName;
      private String toUserName;
      private String content;
      ...
  }
  </code></pre>
  
  <p>When a new message is created in my <code>MessageService</code>, I want to inform the receiver if he is connected. I think I have to add a method <code>WebSocketEndpoint.onServerMessage</code>:</p>
  
  <pre><code>public void onServerMessage(Session session, Message message) {
      session.getBasicRemote().sendText(message.getContent());
  }
  </code></pre>
  
  <p>But I don't know how to do something like that which works.</p>