1

I'm trying to respond to an unauthenticated user using @SendToUser.

  • Spring 4.1.1

I'm using a newly created Spring Boot application and the only config I have is:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp").withSockJS();
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); 
    }
}

Controller code:

@MessageMapping("/search")
@SendToUser("/search")
public String search(@Payload String xxx) {
    return "TEST1234";
}

JS:

var socket = new SockJS('/webapp/stomp');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
    stompClient.subscribe('/user/search', function(data){
        alert(data.body);
    });
});

Spring output:

DEBUG  org.springframework.web.servlet.DispatcherServlet: 996 - Successfully completed request
DEBUG   o.s.w.s.handler.LoggingWebSocketHandlerDecorator:  45 - New WebSocketServerSockJsSession[id=shizav88]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=shizav88
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/search -> [/search-usershizav88]

JS:

var name = document.getElementById('name').value;
stompClient.send("/app/search", {}, "test"));

Spring output:

DEBUG     o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 348 - Searching methods to handle SEND /app/search session=shizav88
DEBUG     o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 446 - Invoking de.benneq.next.search.SearchController#search[1 args]
DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 247 - Returning cached instance of singleton bean 'searchController'
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/shizav88/search -> [/search-usershizav88]

To me it looks like Spring maps the subscription to /search-usershizav88. And then uses /search-usershizav88 to send the response. But my JS client does not receive anything.

What's the problem?

Benjamin M
  • 23,599
  • 32
  • 121
  • 201

2 Answers2

10

Now I got it working, but I don't really know WHY:

Config:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp").withSockJS();
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/queue", "/topic"); // <- added "/queue"
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); 
    }
}

Controller:

@MessageMapping("/search")
@SendToUser // <- maps to "/user/queue/search"
public String search(@Payload String xxx) {
    return "TEST1234";
}

JS:

stompClient.subscribe('/user/queue/search', function(data){
    alert(data.body);
});

Spring output:

DEBUG  org.springframework.web.servlet.DispatcherServlet: 996 - Successfully completed request
DEBUG   o.s.w.s.handler.LoggingWebSocketHandlerDecorator:  45 - New WebSocketServerSockJsSession[id=fkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=fkbmnpkj
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/queue/search -> [/queue/search-userfkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 175 - Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

Now there's an additional line in the log, which says that it is Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

That was missing before.

It would still be nice, if someone could explain why I need the /queue mapping for this to work properly.

Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • 1
    What the UserDestinationResolver makes is removing '/user' prefix. Then in the non-working case the destination remianed was '/search/{'user'+userSessionId}', but '/search' is filtered out, because there was not simpleBroker is enabled there. – Mert Mertce Apr 01 '15 at 09:45
  • thanks for pointing me to adding **"/queue"** here `registry.enableSimpleBroker("/queue", "/topic"); // <- added "/queue"` – Shantaram Tupe Dec 02 '17 at 14:24
  • 1
    How did you enable all this debug output - my spring isn't posting much – Jeef Apr 04 '18 at 17:31
0

In first post you subscribe at "/user/search". In second "/user/queue/search". I checked "/user/topic/search" and it also works. So solution is to use valid topic name. No magic...

Jason
  • 2,503
  • 3
  • 38
  • 46
Igor Cherepov
  • 51
  • 1
  • 1