title: How to send message to client through websocket using Spring
body: <p>I try to use Spring with websocket. I started my investigation with <a href="https://spring.io/guides/gs/messaging-stomp-websocket/" rel="noreferrer">this tutorial</a>.</p>
  
  <p>In my side client I have something like that to initialize the connection to the server :</p>
  
  <pre><code>function connect() {
      var socket = new SockJS('/myphotos/form');
      stompClient = Stomp.over(socket);
      stompClient.connect({}, function(frame) {
          setConnected(true);
          console.log('Connected: ' + frame);
          stompClient.subscribe('/topic/greetings', function(greeting){
              showGreeting(JSON.parse(greeting.body).content);
          });
      });
  }
  </code></pre>
  
  <p>It works great, in my controller I’m able to do my process in the following class :</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class PhotoController {
  
      @MessageMapping("/form")
      @SendTo("/topic/greetings")
      public Greeting validate(AddPhotosForm addPhotosForm) {
          return new Greeting("Hello world !");
      }
  }
  </code></pre>
  
  <p>Now what I want to do it’s having a thread sending a message to the client listening on “/topic/greeting”. I wrote my Runnable class like this :</p>
  
  <pre><code>public class FireGreeting implements Runnable {
  
      private PhotoController listener;
  
      public FireGreeting(PhotoController listener) {
          this.listener = listener;
      }
  
      @Override
      public void run() {
          while (true) {
              try {
                  Thread.sleep( 2000 );
                  listener.fireGreeting();
              } catch ( InterruptedException e ) {
                  e.printStackTrace();
              }
          }   
      }
  }
  </code></pre>
  
  <p>And completed my controller like that :</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class PhotoController {
  
      @MessageMapping("/form")
      @SendTo("/topic/greetings")
      public Greeting validate(AddPhotosForm addPhotosForm) {
  
          // added this part
          FireGreeting r = new FireGreeting( this );
          new Thread(r).start();
  
          return new Greeting("Hello world !");
      }
  
      // added this method
      @SendTo("/topic/greetings")
      public Greeting fireGreeting() {
          System.out.println("Fire");
          return new Greeting("Fire");
      }
  }
  </code></pre>
  
  <p>The method PhotoController.fireGreeting is called as I want but nothing happened on the client side.
  Any suggestions ? Thank you.</p>