Search code examples
javaspringjettyspring-websocket

What are other WebsocketClient implementations in Spring?


I have a Spring application which is connecting to the websocket endpoint. For this I use Spring StandardWebSocketClient class.

WebSocketClient transport = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(transport);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
String url = "ws://localhost:8080/asynccontrol";
StompSessionHandler handler = new MySessionHandler();
stompClient.connect(url, handler);

This works in Jetty without any problem. Now I have to deploy this application in Websphere JRE7 environment. As this WAS version does not support JSR-356, I have to use another implementation. From this link WebsocketClient can be of type:

  • StandardWebSocketClient in a JSR-356 runtime
  • JettyWebSocketClient using the Jetty 9+ native WebSocket API
  • Any implementation of Spring’s WebSocketClient

First choice unavailable (no JSR-356 runtime), second choice unavailable (needs JRE8, getting unsupported major.minor version 52), what is this third choice? What are other implementations of Spring's WebSocketClient?

May it be possible to recompile org.eclipse.jetty.websocketclient in JRE7?

Solution

I cannot use jetty9.2 because it is not compatible with JettyWebSocketClient adapter. However I could just possibly use the api without adapter, with StandardWebSocketClient.

I installed tomcat8 and it works with StandardWebSocketClient too. In tomcat7 I was still getting incompatible WsContainerProvider subtype error, maybe a bug for my specific tomcat version. For the backup plan I have also developed TyrusWebSocketClient. Thank you guys.


Solution

  • You can use tyrus-standalone client.

    If you are using Maven, add this dependency :

    <dependency>
         <groupId>org.glassfish.tyrus.bundles</groupId>
         <artifactId>tyrus-standalone-client</artifactId>
         <version>1.9</version>
    </dependency>
    

    or

     <dependency>
         <groupId>org.glassfish.tyrus.bundles</groupId>
         <artifactId>websocket-ri-bundle</artifactId>
         <version>1.2.1</version>
     </dependency>
    

    I have used websocket-ri-bundle (second dependency) it works, but any one of these two will work fine.

    EDIT

    If you see StandardWebsocketClient class

    public StandardWebSocketClient() {
        this.webSocketContainer = ContainerProvider.getWebSocketContainer();
    }
    

    Spring tries to identify the Container provider from META-INF/services/javax.websocket.ContainerProvider file of client implementation. So any websocket client implementation which correctly points out this file will work.