On this page
6. WebSocket Integration
Spring Session provides transparent integration with Spring’s WebSocket support.
| ![[Note]](/images/spring-session/2.1.2.RELEASE/note.png) | Note | 
|---|---|
| Spring Session’s WebSocket support only works with Spring’s WebSocket support. Specifically it does not work with using JSR-356 directly. This is due to the fact that JSR-356 does not have a mechanism for intercepting incoming WebSocket messages. | 
So why do we need Spring Session when using WebSockets?
Consider an email application that does much of its work through HTTP requests. However, there is also a chat application embedded within it that works over WebSocket APIs. If a user is actively chatting with someone, we should not timeout the HttpSession since this would be pretty poor user experience. However, this is exactly what JSR-356  does.
Another issue is that according to JSR-356 if the HttpSession times out any WebSocket that was created with that HttpSession and an authenticated user should be forcibly closed. This means that if we are actively chatting in our application and are not using the HttpSession, then we will also disconnect from our conversation!
The WebSocket Sample provides a working sample on how to integrate Spring Session with WebSockets. You can follow the basic steps for integration below, but you are encouraged to follow along with the detailed WebSocket Guide when integrating with your own application:
Before using WebSocket integration, you should be sure that you have Chapter 5, HttpSession Integration working first.
In a typical Spring WebSocket application users would implement WebSocketMessageBrokerConfigurer. For example, the configuration might look something like the following:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("/messages").withSockJS();
	}
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableSimpleBroker("/queue/", "/topic/");
		registry.setApplicationDestinationPrefixes("/app");
	}
}We can easily update our configuration to use Spring Session’s WebSocket support. For example:
src/main/java/samples/config/WebSocketConfig.java.
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig
		extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> {  @Override
	protected void configureStompEndpoints(StompEndpointRegistry registry) {
	@Override
	protected void configureStompEndpoints(StompEndpointRegistry registry) {  registry.addEndpoint("/messages").withSockJS();
	}
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableSimpleBroker("/queue/", "/topic/");
		registry.setApplicationDestinationPrefixes("/app");
	}
}
		registry.addEndpoint("/messages").withSockJS();
	}
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableSimpleBroker("/queue/", "/topic/");
		registry.setApplicationDestinationPrefixes("/app");
	}
}To hook in the Spring Session support we only need to change two things:
What does AbstractSessionWebSocketMessageBrokerConfigurer do behind the scenes?
- WebSocketConnectHandlerDecoratorFactoryis added as a- WebSocketHandlerDecoratorFactoryto- WebSocketTransportRegistration. This ensures a custom- SessionConnectEventis fired that contains the- WebSocketSession. The- WebSocketSessionis necessary to terminate any WebSocket connections that are still open when a Spring Session is terminated.
- SessionRepositoryMessageInterceptoris added as a- HandshakeInterceptorto every- StompWebSocketEndpointRegistration. This ensures that the Session is added to the WebSocket properties to enable updating the last accessed time.
- SessionRepositoryMessageInterceptoris added as a- ChannelInterceptorto our inbound- ChannelRegistration. This ensures that every time an inbound message is received, that the last accessed time of our Spring Session is updated.
- WebSocketRegistryListeneris created as a Spring Bean. This ensures that we have a mapping of all of the Session id to the corresponding WebSocket connections. By maintaining this mapping, we can close all the WebSocket connections when a Spring Session (HttpSession) is terminated.