How to connect to Spring WebSocket request in backend?

Viewed 2691

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.

1 Answers

If you want to connect to wss//:localhost:8085/mywebsockets you have to set the entrypoint to this url, not to addEndpoint("/api"). You should allow cross origin requests for testing:

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/mywebsockets").setAllowedOrigins("*").withSockJS();
    }

You can use a chrome extension like this to test your websocket connection: https://chrome.google.com/webstore/detail/apic-complete-api-solutio/ggnhohnkfcpcanfekomdkjffnfcjnjam

Choose api testing -> ws and connect to http://localhost:8085/mywebsockets, the client will switch the protocol automatically to ws://

This tutorial helped me set up everything correctly: http://kojotdev.com/2019/07/using-spring-websocket-stomp-application-with-vue-js/

Related