Search code examples
stompspring-websocketcloud-foundrysockjs

Websockets connection fails on Pivotal Cloud but works on localhost


I have a Java Spring Boot based web service called cde-service and an Angular 5 based front end called abc-app.

abc-app uses a websocket connection to send logging data to cde-service that in turn does something with the client side log data.

I have tested the applications on localhost and they work as expected.

However when I deploy cde-service onto Pivotal Cloud Foundry, the websocket connection fails with 403 Forbidden. I have done the changes to allow CORS.

Any pointers as to why this is happening would be really helpful.

cde-service :

// Snippet from the Websocket Configuration. 

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


@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {

  registry.enableSimpleBroker("/topic", "/queue");
  registry.setApplicationDestinationPrefixes("/app");
}



// Snippet from the Controller.

@MessageMapping("/logs")
public void handleLogs(LogModel logModel) {
  logService.logHandler(logModel);
}

abc-app :

var sock = new SockJS("http://host_name/websocketConnection");
var stomp = Stomp.over(sock);

stomp.connect('guest', 'guest', function(frame) {
  console.log('----    Established Connection  ----');
});

stomp.send("/app/logs", {},
  JSON.stringify({
    'logData': 'Some Data'
  }));


Solution

  • However when I deploy cde-service onto Pivotal Cloud Foundry, the websocket connection fails with 403 Forbidden. I have done the changes to allow CORS.

    Cloud Foundry does not add any stipulations for using WebSockets in your app. The standard browser client should work fine. In this case, your client app is attempting to connect to your server app and your server app is returning a 403. The only way you can figure out why is to debug your server app and see why it's saying that the WebSocket connection is forbidden.

    The platform would not be generating the 403.

    To me CORS is the most likely culprit, but there's by no means enough information here for me to make that determination and it could be other things as well. Turn up the log levels in your app and see what's happening. Also, try testing locally but using multiple domains so that you actually have to deal with CORS.

    The only other thing to watch out for here, is that some installations of Cloud Foundry require your WebSocket connections to go over a different port. This is not a limitation of Cloud Foundry, but of the load balancer used by your platform operator. In some cases, like when running on AWS, the load balancers (ELB, cough) do not support having HTTP, HTTPS and WebSockets traffic all over the same port. To work around this load balancer limitation, operators will have separate ports for each protocol, i.e. HTTP -> 80, HTTPS 443, WSS 4443. Check with your platform operator to see how your environment was deployed.