I'm trying to add a websocket connection to my spring boot application, I'm used to deal with Https protocol only, but I need to have this websocket connection to keep sending notifications to frontend. I've added this dependency <org.springframework.boot:spring-boot-starter-websocket:2.0.0.RELEASE>, because for some reason my application doesn't execute version 2.4.5 of spring-starter-websocket.
I have one configuration class to enable this connection, here's this class code:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/api").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config){
config.enableSimpleBroker("/topic/");
}
}
To handle the connection I've created this method in my controller:
@Autowired
SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping("/news")
public void broadcastNews(@Payload String message) {
this.simpMessagingTemplate.convertAndSend("/topic/news", message);
}
To test this connection I'm using this site: https://www.websocket.in/test-online, and I'm trying to connect to this url: wss//:localhost:8085/mywebsockets
My console responds this when I try to connect:
2020-02-25 10:32:09.707 INFO 16000 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 3, active threads = 1, queued tasks = 2, completed tasks = 0]
The site doesn't connect but it doesn't get any error, I've waited more than 10 minutes and nothing else happens.
I don't understand what I'm doing wrong, I've tried connecting with Https already and it doesn't work at all, maybe I'm not finding how my url must be, because my application gets the request for the connection, as it responds in the console.
I'm trying to connect to this url: wss//:localhost:8085/mywebsockets. The website will look for a websocket server on it's host machine but not on your machine. You should tell the website to connect to YOUR public ip. You can get your IP from here whatismyip.com