title: spring web socket server and react client
body: <p>i have to develop a web socket server with spring that send to the client a message avery 5 sec. The client is written in react js. This is my server code:</p>
  
  <pre><code>@SpringBootApplication
  @EnableAsync
  @EnableScheduling
  public class TestwsApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(TestwsApplication.class, args);
      }
  }
  
  
  
  @Configuration
  @EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry config) {
          config.enableSimpleBroker("/topic");
      }
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint("/chatWS").setAllowedOrigins("*").withSockJS();
      }
  </code></pre>
  
  <p>This is my scheduler that send to the channel /topic/message a message every 5 sec</p>
  
  <pre><code>@Component
  public class ScheduledTasks {
  
      @Autowired
      WebSocketListener listener;
  
      int i=0;
  
      private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
  
      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  
      @Scheduled(fixedRate = 5000)
      public void reportCurrentTime() {
  
          if (i==0){
  
              listener.pushSystemStatusToWebSocket("ok");
              i=1;
          }else{
              listener.pushSystemStatusToWebSocket("errore");
              i=0;
          }
  
  
  
      }
  }
  </code></pre>
  
  <p>This is my service used by the scheduler to send the message to the clients </p>
  
  <pre><code>    @Service
  public class WebSocketListener {
  
      @Autowired
      private SimpMessagingTemplate webSocket;
  
  
      @Async
      public void pushSystemStatusToWebSocket (String newStatus){
          System.out.println("inviooooooooooooooooooooooooooo");
  
          webSocket.convertAndSend("/topic/messages", newStatus);
      }
  
  }
  </code></pre>
  
  <p>This is my react component</p>
  
  <pre><code>import SockJS from 'sockjs-client';
  
  class Main extends React.Component {
      constructor() {
          super();
          this.state = {
              clickCount: 0,
          };
      }
      componentDidMount(){
          // this is an "echo" websocket service
          console.log('didmount')
          var sock = new SockJS('http://localhost:8080/chatWS');
  
  
          sock.onopen = function() {
              console.log('open socket ');
              sock.send('test');
          };
  
          sock.onmessage = function(e) {
              console.log('message');
              console.log('message', e.data);
              sock.close();
          };
  
          sock.onclose = function() {
              console.log('close');
          };
      }
  </code></pre>
  
  <p>on the log i see only the post open socket ... i dont see any log inserted in onmessage... so the client did not receive the message. Why ? Can you help me ?
  Thanks
  Esoni</p>