title: How can I send a message on connect event (SockJS, STOMP, Spring)?
body: <p>I am connection through SockJS over STOMP to my Spring backend. Everything work fine, the configuration works well for all browsers etc. However, I cannot find a way to send an initial message. The scenario would be as follows:</p>
  
  <ol>
  <li>The client connects to the topic</li>
  </ol>
  
  <pre>
  
      function connect() {
          var socket = new SockJS('http://localhost:8080/myEndpoint');
          stompClient = Stomp.over(socket);
          stompClient.connect({}, function(frame) {
              setConnected(true);
              console.log('Connected: ' + frame);
              stompClient.subscribe('/topic/notify', function(message){
                  showMessage(JSON.parse(message.body).content);
              });
          });
      }
  
  </pre>
  
  <p>and the backend config looks more or less like this:</p>
  
  <pre>
  
      @Configuration
      @EnableWebSocketMessageBroker
      public class WebSocketAppConfig extends AbstractWebSocketMessageBrokerConfigurer {   
      ...
      @Override
      public void registerStompEndpoints(final StompEndpointRegistry registry) {
          registry.addEndpoint("/myEndpoint").withSockJS();
      }
  
  </pre>
  
  <ol>
  <li>I want to send to the client an automatic reply from the backend (on the connection event) so that I can already provide him with some dataset (e.g. read sth from the db) without the need for him (the client) to send a GET request (or any other). So to sum up, I just want to send him a message on the topic with the SimMessagingTemplate object just after he connected.</li>
  </ol>
  
  <p>Usually I do it the following way, e.g. in a REST controller, when the template is already autowired:</p>
  
  <pre>
  
      @Autowired
      private SimpMessagingTemplate template;
      ...
      template.convertAndSend(TOPIC, new Message("it works!"));
  
  </pre>
  
  <p>How to achieve this on connect event?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have managed to make it work. However, I am still a bit confused with the configuration. I will show here 2 configurations how the initial message can be sent:</p>
  
  <p><strong>1) First solution</strong></p>
  
  <p>JS part</p>
  
  <pre><code>stompClient.subscribe('/app/pending', function(message){
      showMessage(JSON.parse(message.body).content);
  });
  stompClient.subscribe('/topic/incoming', function(message){
      showMessage(JSON.parse(message.body).content);
  });
  </code></pre>
  
  <p>Java part</p>
  
  <pre><code>@Controller
  public class WebSocketBusController {
      @SubscribeMapping("/pending")
  </code></pre>
  
  <p>Configuration</p>
  
  <pre><code>@Override
  public void configureMessageBroker(final MessageBrokerRegistry config) {
      config.enableSimpleBroker("/topic");
      config.setApplicationDestinationPrefixes("/app");
  }
  </code></pre>
  
  <p>... and other calls</p>
  
  <pre><code>template.convertAndSend("/topic/incoming", outgoingMessage);
  </code></pre>
  
  <p><strong>2) Second solution</strong></p>
  
  <p>JS part</p>
  
  <pre><code>stompClient.subscribe('/topic/incoming', function(message){
      showMessage(JSON.parse(message.body).content);
  })
  </code></pre>
  
  <p>Java part</p>
  
  <pre><code>@Controller
  public class WebSocketBusController {
      @SubscribeMapping("/topic/incoming")
  </code></pre>
  
  <p>Configuration</p>
  
  <pre><code>@Override
  public void configureMessageBroker(final MessageBrokerRegistry config) {
      config.enableSimpleBroker("/topic");
      // NO APPLICATION PREFIX HERE
  }
  </code></pre>
  
  <p>... and other calls</p>
  
  <pre><code>template.convertAndSend("/topic/incoming", outgoingMessage);
  </code></pre>
  
  <p>SUMMARY:</p>
  
  <p>The first case uses two subscriptions - this I wanted to avoid and thought this can be managed with one only.</p>
  
  <p>The second one however has no prefix for application. But at least I can have a single subscription to listen on the provided topic as well as send initial message.</p>