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?
Now I got it working, but I don't really know WHY:
Config:
Controller:
JS:
Spring output:
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.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...