Scaling web sockets with Spring Boot

Viewed 254

I'm considering the performance implications of trying to horizontally scale a server which offers websocket connections to clients.

My current implementation of the server uses Spring Boot on the backend, and uses pure web sockets (without STOMP) to transport messages between the server and client.

In a world of containers and horizontal scaling, how should I design the server that it does not matter which replica the client connects to, that delivery of messages is guaranteed?

I am considering using Redis Pub/Sub for this, however, unsure what it should look like.

Currently, my code looks like the following:

@Configuration
@EnableWebSocket
public class WebSocketServerConfiguration implements WebSocketConfigurer {

    @Autowired
    protected ControllerHandler webSocketHandler;

    @Autowired
    private AuthHandshakeInterceptor interceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/ws")
                .addInterceptors(interceptor)
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }
}

And the webSocketHandler looks like:

@Component
public class ControllerHandler extends TextWebSocketHandler {

    @Autowired
    private InitController initController;
    @Autowired
    private ProjectController projectController;

    @Autowired ObjectMapper objectMapper;

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage jsonTextMessage) throws Exception {
        // get the message from a controller
        session.sendMessage(new TextMessage("foo"));
    }
}

I am guessing that in the controller handler, I'll need to publish a message to Redis, and that there should also be a subscriber to actually send the message across the websocket?

0 Answers
Related