8

I need to install an application on a non JEE7 compliant server. I am using Spring + Stomp + SocksJs for realtime notifications.

My code looks like this:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry ser) {
        ser.addEndpoint("/notifications").withSockJS()
    }

   }

}

And on the client:

function setSocket(broker, callbackFn) {

    var socket = {};

    socket.cliente = new SockJS(path + broker);
    socket.stomp = Stomp.over(socket.cliente);

    socket.stomp.connect({}, function () {
        socket.stomp.subscribe("/topic" + broker, callbackFn);
    });

}

Is there any way to manually set the transport type to use and avoid the use of websockets?

pallendes
  • 95
  • 1
  • 1
  • 9

1 Answers1

18

If your server doesn't support websockets, make sure that it still support async handling.

You can disable the websocket transport on the server side with the following option:

@Override
public void registerStompEndpoints(StompEndpointRegistry ser) {
    ser.addEndpoint("/notifications").withSockJS().setWebSocketEnabled(false);
}

SockJS clients will automatically select the best available transport supported by the server. In your case, this is the preferred way to deal with this.

As explained in the SockJS client documentation, you can limit the available transports on the client side with a transports argument when creating the SockJS client.

sockJsProtocols = ["xhr-streaming", "xhr-polling"];
socket.cliente = new SockJS(url, null, {transports: sockJsProtocols});

The full list of available transports is here. But technically, if you're not restricting the available transports on the server side, a client could still attempt to connect using websockets.

Eugene
  • 117,005
  • 15
  • 201
  • 306
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • 1
    Thank u! that was exactly what I needed. – pallendes Oct 14 '16 at 00:06
  • 1
    I've edited my answer - in your case, you want to restrict the available transports *on the server side*. Doing so just on the client works, but does not prevent a client to try connecting to your server with websockets, if that client is configured otherwise. – Brian Clozel Oct 14 '16 at 07:40
  • I've stopped manually the server, after couple of minutes again i've started server, client shows the message like Opening Web Socket..., not connecting websocket – Manohar Pamishetty Apr 26 '22 at 07:40