I have a real-time application with clients using websockets to connect with a Spring Framework server, which is running Spring Boot Tomcat. I want the server to quickly (wi
As the question and the top-voted answer are quite old, wanted to add the easiest way to achieve the same with spring - websocket implementation.
Refer section 4.4.8. Simple Broker
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
private TaskScheduler messageBrokerTaskScheduler;
@Autowired
public void setMessageBrokerTaskScheduler(TaskScheduler taskScheduler) {
this.messageBrokerTaskScheduler = taskScheduler;
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/")
.setHeartbeatValue(new long[] {10000, 20000})
.setTaskScheduler(this.messageBrokerTaskScheduler);
// ...
}
}
When returning the connect response frame, the websocket-server needs to send the hearbeat params to enable ping from the clientside.
If you are using SOCKJS implementation on the clientside, then no additional code is needed to add align to PING/PONG implementation.