title: Sending a message from server to client via Websocket in Spring
body: <p>My requirement is to send(broadcast) a message via web-socket once some change in back end occurs.</p>
  
  <p>I followed <a href="https://spring.io/guides/gs/messaging-stomp-websocket/" rel="nofollow noreferrer">this tutorial</a> and utilized the answer in <a href="https://stackoverflow.com/questions/28250719/how-to-send-message-to-client-through-websocket-using-spring">this question</a> to come up with a solution that send a message by periodically calling a method.</p>
  
  <p>My web socket controller class - GreetingController class is like this,</p>
  
  <pre><code>@Controller
  public class GreetingController {
  
      @Autowired
      private SimpMessagingTemplate template;
  
      @MessageMapping("/hello")
      @SendTo("/topic/greetings")
      public Greeting greeting(HelloMessage message) throws Exception {
          FireGreeting r = new FireGreeting( this );
          new Thread(r).start();
          return new Greeting("Hello world !");
      }
  
      public void fireGreeting() {
          System.out.println("Fire");
          this.template.convertAndSend("/topic/greetings", new Greeting("Fire"));
      }
  }
  </code></pre>
  
  <p>fireGreeting method is called in the thread class - FireGreeting</p>
  
  <pre><code>public class FireGreeting implements Runnable {
  
      private GreetingController listener;
  
      public FireGreeting( GreetingController listener) {
          this.listener = listener;
      }
  
      @Override
      public void run() {
          while (true) {
              try {
                  Thread.sleep( 2000 );
                  listener.fireGreeting();
              } catch ( InterruptedException e ) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>When i run this project i can send a message initially and then the thread starts periodically calling the fireGreeting() method. </p>
  
  <p>What i wan't is to without calling the greeting(HelloMessage message) method,
  call the fireGreeting() method whenever my back-end operation is run.</p>
  
  <p>I tried calling the method separately but it gives a null pointer exception because at that point <code>SimpMessagingTemplate template;</code>  is not initialized.</p>
  
  <p>My questions are as follows,
  1.When does the template object get initialized? 
  (So i can do the same thing and use it to call convertAndSend() method)</p>
  
  <ol start="2">
  <li><p>Is it impossible to call the convertAndSend method like the way im trying to?</p></li>
  <li><p>Is there any other way i can achieve this requirement in spring and websocket?</p></li>
  </ol>
  
  <p>I also tried <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket-stomp-handle-send" rel="nofollow noreferrer">this code in the documentation</a>, but i don't have a clear understanding on how it works. 
  It seems like, i can send a POST call and invoke the greet() method so it sends a message to server.
  But what i want is to invoke the method solely from the server side.</p>
  
  <p>Any advice regarding this would be a great help. :)</p>