Manually set transport type on a Spring + Sockjs application
Asked Answered
I

1

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?

Intended answered 12/10, 2016 at 12:28 Comment(0)
G
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.

Gaidano answered 13/10, 2016 at 18:42 Comment(3)
Thank u! that was exactly what I needed.Intended
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.Gaidano
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 websocketDaniel

© 2022 - 2024 — McMap. All rights reserved.