Websocket over StompJS not able to send message to host, running Springboot

Discussão em 'Angular' iniciado por plex4r, Abril 9, 2025.

  1. plex4r

    plex4r Guest

    I have an Angular fontend application with a Spring Boot backend that uses websockets over Stomp. The app can successfully send websocket messages from the host to the browser. I need to now send messages from the browser but the message is failing to show up on my Spring controller containing the MessageMapping.

    Here is my wesocket configuration:

    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/connectws")
    .addInterceptors(new HttpHandshakeInterceptor())
    .setAllowedOriginPatterns("*")
    .withSockJS();
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableSimpleBroker("/topic");
    }
    }


    Here is my message handler on the backend:

    @Controller
    public class WebsocketController {
    @MessageMapping("/sendToHost")
    public void sendToHost( Object message) {
    System.out.println("Received message: " + message );
    }


    In the browser code my stompClient is able to successfully subscribe to destinations and receive messages. I use this same stompClient to attempt to publish a message to ‘/app/sendToHost’. However, the message never arrives in my “sendToHost()” Java method. I have traced through the UI publish code and it indicates that the stomClient is connected and it appears to finally send the message.

    Here is my publish code.

    this.stompClient.publish({
    destination: '/app/sendToHost',
    headers: { },
    body: 'message',
    });


    Any ideas or suggestions on how I can get this working. I have not included all code to just focus on what I believe to be the important code, but please let me know if additional code will help and I will gladly add it.

    Continue reading...

Compartilhe esta Página