2

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
  • 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 Commented Feb 26, 2020 at 4:35

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.