-1

Hello everyone I'm having some issues figuring out some websocket endpoints. I am trying to build a basic chat application to help me gain an understanding of websockets.

I'm running a Java 8 Spring Boot Websocket server that is running on http://localhost:8080 for testing purposes.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketMessageConfig implements WebSocketMessageBrokerConfigurer {


    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        WebSocketMessageBrokerConfigurer.super.registerStompEndpoints(registry);
        registry.addEndpoint("/websocket").setAllowedOriginPatterns("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(final MessageBrokerRegistry registry) {
        WebSocketMessageBrokerConfigurer.super.configureMessageBroker(registry);
        registry.setApplicationDestinationPrefixes("/app");
        registry.enableSimpleBroker("/topic");
    }
}

@Controller
public class SocketController {



    @MessageMapping("/socket.send")
    @SendTo("/topic/public")
    public SocketMessage sendMessage(@Payload final SocketMessage message) {
        return message;
    }

    @MessageMapping("/socket.newUser")
    @SendTo("/topic/public")
    public SocketMessage newUser(@Payload final SocketMessage message,
                                 SimpMessageHeaderAccessor  headerAccessor) {

        headerAccessor.getSessionAttributes().put("username", message.getSender());
    return message;
    }

}

That's my config file and controller file... My understanding is that the registerStompEndpoints() is giving the endpoint to setup the connection between client/server for a session.

The front end request will look like

That seems to work fine from my javascript front end request. I see a good log showing a connection to that, but when I try to send a newUser or a message, neither appear to be properly going to the server. I don't see anything in the logs so I suspect my endpoints from the front-end are incorrect...

const socket = new SockJS('http://localhost:8080/websocket')
stompClient.subscribe('http://localhost:8080/topic/public', onMessageReceived)
stompClient.send("http://localhost:8080/app/socket.newUser",
stompClient.send("http://localhost:8080/app/socket.send", {}, JSON.stringify(chatMessage))

This is my console.log from the from the front end

>>> SUBSCRIBE
id:sub-0
destination:http://localhost:8080/app/topic/public

>>> SEND
destination:http://localhost:8080/app/socket.newUser
content-length:36

{"sender":"testname","type":"CONNECT"}
  • 1
    The idea is that you set up the websocket connection, and then you communicate directly through that. So you don't send things by using URLs; once established the web socket has no idea what a URL is. Also, all modern browsers have native websocket support, no need for SockJS, just create a [normal client-side WebSocket connection](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications) – Mike 'Pomax' Kamermans Aug 26 '22 at 15:39
  • Thank you for that suggestion. I made the changes and it is now properly working. Credit to you for the answer! – Justin Garner Aug 26 '22 at 16:02

1 Answers1

0

Thanks to Mike 'Pomax' Kamermans for the solution.

My error was in my front end code.

This was my original setup.

const socket = new SockJS('http://localhost:8080/websocket')
stompClient.subscribe('http://localhost:8080/topic/public', onMessageReceived)
stompClient.send("http://localhost:8080/app/socket.newUser",
stompClient.send("http://localhost:8080/app/socket.send", {}, JSON.stringify(chatMessage))

It was changed to look like ....

const socket = new SockJS('http://localhost:8080/websocket')

stompClient.subscribe('/topic/public', onMessageReceived)

stompClient.send("/app/socket.newUser",

stompClient.send("/app/socket.send", {}, JSON.stringify(chatMessage))```


This was the cause of my issues. Thanks for the help!
  • If this is the correct answer please mark it as such so it's clear to others who have this same question in the future. Thanks! – Justin Bertram Aug 27 '22 at 17:02