I have application which uses Spring 4.3.5 and spring mvc - apache tiles .
I wrote chat according to this article https://spring.io/guides/gs/messaging-stomp-websocket/
Everything is working correctly, if my whole application context path is root so for example : http://example.com/ I recieve following frames in websocket
["SUBSCRIBE\nid:sub-0\ndestination:/chat-messages/TST\n\n\u0000"]
["SEND\ndestination:/chat/message/TST\ncontent-length:52\n\n{\"message\":\"\",\"username\":\"USER\",\"event\":\"ONLINE\"}\u0000"]
["MESSAGE\ndestination:/chat-messages/TST\ncontent-type:application/json;charset=UTF-8\nsubscription:sub-0\nmessage-id:x1jpjyes-1\ncontent-length:230\n\n{..SOME JSON CONTENT....}\u0000"]
Problem is that it stops working, If I add some app context ( and I need to do so on my server)
for example : http://example.com/my-app
No messages received , nor sent
UPDATE: No sending was fixed by adding servletContext.getContextPath() to destination prefixes.
With context, I only got this:
["SUBSCRIBE\nid:sub-0\ndestination:/my-app/chat-messages/TST\n\n\u0000"]
["SEND\ndestination:/my-app/chat/message/TST\ncontent-length:52\n\n{\"message\":\"\",\"username\":\"USER\",\"event\":\"ONLINE\"}\u0000"]
Here is my configurations:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
private static final String TILES = "/WEB-INF/tiles/tiles.xml";
private static final String VIEWS = "/WEB-INF/views/**/views.xml";
private static final String RESOURCES_HANDLER = "/resources/";
private static final String RESOURCES_LOCATION = RESOURCES_HANDLER + "**";
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping requestMappingHandlerMapping = super
.requestMappingHandlerMapping();
requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
return requestMappingHandlerMapping;
}
@Bean
public TilesViewResolver configureTilesViewResolver() {
return new TilesViewResolver();
}
@Bean
public TilesConfigurer configureTilesConfigurer() {
TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(TILES, VIEWS);
return configurer;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(
RESOURCES_LOCATION);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebSocketMesssageBroker
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
@Autowired
private ServletContext servletContext;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/chat-messages");
config.setApplicationDestinationPrefixes(servletContext.getContextPath() + "/chat");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat-websocket").withSockJS();
}
}
And I have a controller to process everything
@MessageMapping("/message/{projectId}")
@SendTo("/chat-messages/{projectId}")
public ChatResponse sendMessage(@DestinationVariable String projectId, MessageSent message) throw InterruptedException {
//Send reponse back like user online/offline or message posted
return new ChatResponse(chatMessage);
}
In JSP file I have following JS called
var socket = new SockJS('<c:url value="/chat-websocket/"/>');
stompClient.subscribe('<c:url value="/chat-messages/${chatProject.projectId}"/>', function (data) { ....SOME RESPONSE PROCESSING... });
stompClient.send("<c:url value="/chat/message/${chatProject.projectId}"/>", {}, JSON.stringify({.....PAYLOAD TO SEND ---}));
and web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<exception-type>org.springframework.security.web.authentication.rememberme.CookieTheftException</exception-type>
<location>/signin</location>
</error-page>
<error-page>
<location>/generalError</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>
I do suspect that this might be something with configuration of either tiles or whole dispatcher in web.xml or something like this :/
Would be very greatfull for hints
I was finnaly able to resolve this issue. It turns out whenever I was creating SockJS subscriber I should pass relative path as param, without any context
(I presume the base websocket opened url already have correct url)
So in order to properly receive subscription events all I had to do was change
stompClient.subscribe('<c:url value="/chat-messages/${chatProject.projectId}"/>', function (data) { ....SOME RESPONSE PROCESSING... });
to this:
stompClient.subscribe('/chat-messages/${chatProject.projectId}', function (data) { ....SOME RESPONSE PROCESSING... });
(without the <c:url> which was returning context path all the time)
So whenever I tried to subscribe to chat-messages using
<c:url value="chat-messages/ID">in fact I was subscribing to:
my-app/chat-messages/IDand my controller and config was expecting plain relative chat-messages
That's why after adding contextPath to WebSocketController setApplicationDestinationPrefixes, app started sending correct messages
Those are couple of hours I'm not getting back :)