Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually set transport type on a Spring + Sockjs application

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?

like image 638
pallendes Avatar asked Oct 12 '16 12:10

pallendes


1 Answers

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.

like image 66
Brian Clozel Avatar answered Sep 23 '22 08:09

Brian Clozel