Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring STOMP Websockets: any way to enable permessage-deflate on server side?

I'm working with spring-websockets under the spring-boot-starter 1.3.1.RELEASE, with the Jetty backend. I am wondering how to enable permessage-deflate in the server.

I have a client hosted on a version of Firefox that is willing to negotiate the compression, ie the initial handshake to the WebSocket endpoint includes the compression negotiation header, like:

GET https://my-websocket-host/my-endpoint
...
Sec-WebSocket-Protocol: v10.stomp,v11.stomp
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: ...
...

, but the server response does not include the permessage-deflate extensions header in the upgrade response, meaning it is not willing to negotiate compression. I have gone on a scavenger hunt for where this could be enabled in configuration but have not found anything. Is there some API I can use to turn this feature on, or is it not supported in the current product?

Thanks very much,

Steve

like image 956
steveg103 Avatar asked Sep 20 '25 18:09

steveg103


1 Answers

This feature needs to be enabled in Jetty itself, as I believe this extension is not registered by default. Spring provides a way to configure the handshake handler and enable that extension.

The principle is explained in the reference documentation on websocket server configuration, but here's a complete example:

@Configuration
@EnableWebSocket
public class SampleJettyWebSocketsApplication implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // make sure to use the handshake handler we've defined
        registry.addHandler(echoWebSocketHandler(), "/echo")
                .setHandshakeHandler(handshakeHandler()).withSockJS();
    }

    @Bean
    public DefaultHandshakeHandler handshakeHandler() {

        WebSocketServerFactory factory = new WebSocketServerFactory();
        // add the "permessage-compress" Websocket extension
        factory.getExtensionFactory()
               .register("permessage-compress", PerMessageDeflateExtension.class);
        return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(factory));
    }
like image 65
Brian Clozel Avatar answered Sep 22 '25 10:09

Brian Clozel