Search code examples
javawebsocketglassfishwildfly-8

how to create standalone tyrus client for websockets inside the wildfly server to communicate between two wildfly instances


  1. Trying to make a websocket connection between two separate deployable wars on two different wildfly instances.
  2. Using tyrus-standalone-client to make websocket connection to the another war.

  3. Below is the snippet used to make web-socket connection

Question : Could be it possible to make the websocket connection to another deployable war ? I couldnt find any exceptions when I ran and not even 101-404 errors (Handshake Errors).

    AuthConfig authConfig = AuthConfig.Builder.create().disableProvidedDigestAuth().build();
    Credentials credentials = new Credentials("root", "xyz");
    ClientManager client = ClientManager.createClient();
    client.getProperties().put(ClientProperties.AUTH_CONFIG, authConfig);
    client.getProperties().put(ClientProperties.CREDENTIALS, credentials);


    try (Session session = client.connectToServer(GovernorNodeWebSocketClient.class, new URI("ws://10.203.67.168:8080/xyz"))) {

        session.getBasicRemote().sendObject("xyz");
        System.out.println("4sendMessageToRemoteGovernor :: sent the message from device services to the remote governor"+responseObject.getJson());
    }
    catch (Exception e) {

        e.printStackTrace();
    }

Solution

  • Yes its possible when I used below snippets, after removing credentials part and ClientManager.createClient();

    So by adding the tyrus dependencies over your wildfly instance we can setup websocket connection between them

    try  {
            System.out.println("sendMessageToRemoteGovernor :: "+message.getJson());
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            session = container.connectToServer(XYZ.class, new URI("ws://localhost:8080/xyz/xyz"));
            session.getBasicRemote().sendObject(message);
        }
        catch (Exception e) {
            e.printStackTrace();
        }