3
votes

Given a websocket configuration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.setApplicationDestinationPrefixes("/app");
        registry.enableSimpleBroker("/queue", "/topic");
    }

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

and client javascript:

<script type="text/javascript">
    console.log('begin javascript');

    var stompClient = null;

    function connect() {
        var socket = new SockJS('/myapp/add');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            console.log('Connected to STOMP: ' + frame);
            stompClient.subscribe('/user/topic/abc', function(calResult) {
                console.log('*** Got it ***');
            });
        });
    }

    connect();

</script>

and sending this message from the server:

messagingTemplate.convertAndSendToUser(username, "/topic/abc", "hello");

the callback never gets fired.

The javascript console shows that the connection is made:

Connected to STOMP: CONNECTED user-name:jschmoe heart-beat:0,0 version:1.1 SUBSCRIBE id:sub-0 destination:/user/topic/abc

and the tomcat console shows:

Processing SUBSCRIBE destination=/topic/abc-useryl3ovhr2 subscriptionId=sub-0 session=yl3ovhr2 user=jschmoe payload=byte[0]

and then when the message is sent:

Processing MESSAGE destination=/topic/abc-useryl3ovhr2 session=null payload=hello

Seems like everything works except for the callback.

1
Have you found a solution to this problem, i might be having a similar issue and this would help.Bojan Trajkovski
It works in a Spring Boot app, but not in another Spring MVC app. At some point we will convert the second app to Spring Boot. I'd like to know, though, exactly what the issue is so I'm trying to eliminate different dependencies one by one.Paul Croarkin
Looks similar to mine problem, I also had a spring boot app made as proof of concept that worked well with sockjs javascript client. I also tested this problematic Spring backend with convertAndSend to that javascript client and didn't received the message, so it's most likely a Spring problem. Please update this question if you make progress or find solution.Bojan Trajkovski
I thought that it might be a Tomcat version since the MVC app was using Tomcat 7 and Boot has Tomcat 8 bundled, but now that I've switched the MVC app to run on 8, it still doesn't work. There are a lot of dependencies, so it may take a while to figure this out.Paul Croarkin

1 Answers

1
votes

In my case the problem was caused by XML configuration, once I switched to Java config with @EnableWebSocketMessageBroker annotation I received the messages on client side.