spring-websocketlong-pollingcloud-foundryspring-session

Spring boot Websocket scaling


I have a spring boot application using web-sockets and stomp, I have to use the xhr-polling protocol because of limitations to our ISAM setup and this application will be hosted on Pivotal Cloud Foundry (PCF).

When I run a single instance, with the following code (below) everything works fine.

Server

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/dummy");
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry
            .addEndpoint("/dummyendpoint")
            .setAllowedOrigins("*")
            .withSockJS();
    }
}

Client

var socket,client;
socket = new SockJS('http://localhost:8080/dummyendpoint');
client = Stomp.over(socket);

client.connect({}, function () {
  client.subscribe('/dummy/message', function (message) {
    console.log('subscribed');
  }
});

However, if I scale up to 2 instances the web-socket connection start to fail:

GET localhost:8080/dummyendpoint/info -> Status 200
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr -> Status 200 OK
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr_send -> Status 204
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr_send -> Status 404

I've been looking at other problems posted, Spring Websocket in a tomcat cluster for example and I implemented their solution but with no success.

I have been reading up on spring-session, which looks to have support for web-sockets (if I read it right?).
I've tried using spring-session with Redis and with/without RabbitMQ as the message broker:

public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session>{

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
         registry.setApplicationDestinationPrefixes("/app");
         registry
                .enableStompBrokerRelay("/topic")
                .setRelayHost("localhost")
                .setRelayPort(61613)
                .setClientLogin("guest")
                .setClientPasscode("guest");
    }

    @Override
    public void configureStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry
            .addEndpoint("/dummyendpoint")
            .setAllowedOrigins("*")
            .withSockJS();
    }
}

Everything I have tried always ends up with the same error:

GET localhost:8080/dummyendpoint/info -> Status 200
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr_streaming -> Status 200 Aborted
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr_send -> Status 204
POST localhost:8080/dummyendpoint/300/jskdncdj/xhr_send -> Status 404

Does anyone know why I am getting this error?
I am guessing it's happening because I'm connecting to instance 1 and the other request are hitting the other instance?

Is it possible for me to scale the application vertically and horizontally while using xhr-polling?

Thanks
Spence


Solution

  • Forgot to update this, stackoverflow.com/a/43174082/4978471 explains why this is not possible.