5

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

spence
  • 166
  • 3
  • 11
  • Take a look at the requests being sent in your browser's DevTools and look at the cookies on the request. See if `JSESSIONID` and `__VCAP_ID__` are being set. These would be a sign that Gorouter is employing sticky sessions and will consistently route your request to the same backend app instance. With WebSockets you don't have to worry about that because it's only one persistent connection, but with long polling you do because it's multiple HTTP requests. If you stick the request to the same backend instance, that might help. – Daniel Mikusa Aug 18 '20 at 16:47
  • 2
    Do keep in mind that Spring's simple stomp broker is in-memory, so it won't work if you want to add more instances (could also be why you're getting a 404). You'll need to use RabbitMQ or some other external stomp broker if you want to be able to legitimately use multiple app instances. This post explains why beautifully. https://stackoverflow.com/a/43174082/1585136 – Daniel Mikusa Aug 18 '20 at 16:50

1 Answers1

2

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

spence
  • 166
  • 3
  • 11