title: How do I send a message in an Spring ApplicationListener (SessionConnectedEvent)
body: <p>I'm using Stomp over SockJS with Spring messaging. I'm trying to send a message to all logged in users when a new user is connected. So first off here's my listener:</p>
  
  <pre><code>@Component
  public class SessionConnectedListener implements ApplicationListener&lt;SessionConnectedEvent&gt; {
  
      private static final Logger log = LoggerFactory.getLogger(SessionConnectedListener.class);
  
      @Autowired
      private SimpMessagingTemplate template;
  
      @Override
      public void onApplicationEvent(SessionConnectedEvent event) {
          log.info(event.toString());
  
          // Not sure if it's sending...?
          template.convertAndSend("/topic/login", "New user logged in");
      }
  
  }
  </code></pre>
  
  <p>My WebSocket configurations</p>
  
  <pre><code>@Configuration
  @EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint("chat").withSockJS();
      }
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry config) {
          config.enableSimpleBroker("/topic", "/queue");
          config.setApplicationDestinationPrefixes("/app");
      }
  
  }
  </code></pre>
  
  <p>My JS config</p>
  
  <pre><code>var socket = new SockJS('/chat');
  stompClient = Stomp.over(socket);
  
  stompClient.connect({}}, function(frame) {
  
      // ... other working subscriptions
  
      stompClient.subscribe("/topic/login", function(message) {
          console.log(message.body);
      });
  
  });
  </code></pre>
  
  <p>My problem here is that my <strong>template.convertAndSend()</strong> doesn't work in the <strong>ApplicationListener</strong>. However, if I put it in a Controller method annotated with @MessageMapping, it will work and I will have a console log client side.</p>
  
  <p>So my question is : Can <strong>template.convertAndSend()</strong> work in an <strong>ApplicationListener</strong>? If so, how? or am I missing something?</p>
  
  <p>Thanks for the help!</p>
  
  <p>PS : my <strong>log.info(event.toString()); works in the ApplicationListener</strong> so I know I'm getting into the onApplicationEvent() method.</p>