WebSocket connection to 'wss://xxxxxxxxx.ws-us.cometchat.io/ws/' failed: WebSocket is closed before the connection is established

I’m using cometchat-pro/chat 2.1.0 in a react web app and I see this error in my browser javascript console in both Chrome 85 and Safari 12 in both development and production:

WebSocket connection to ‘wss://xxxxxxxxx.ws-us.cometchat.io/ws/’ failed: WebSocket is closed before the connection is established.

Here is the relevant code:

var appID = "xxxxxxxxxxxxxx";
var region = "us";
var appSetting = new CometChat.AppSettingsBuilder()
  .subscribePresenceForAllUsers()
  .setRegion(region)
  .build();
CometChat.init(appID, appSetting).then(
  () => {
    if (authToken) {
      CometChat.login(authToken)
        .then(
          (User) => {
            var listenerID = "COMETCHAT_LISTENER_ID";
            CometChat.addConnectionListener(
              listenerID,
              new CometChat.ConnectionListener({
                onConnected: () => {
                  console.log("ConnectionListener => On Connected");
                  var listenerID = participantID;

                  CometChat.addMessageListener(
                    listenerID,
                    new CometChat.MessageListener({
                      onTextMessageReceived: (textMessage) => {
                        console.log(
                          "Text message received successfully",
                          textMessage
                        );
                        // Handle text message
                        addMessageNotification(textMessage);
                      },
                      onMediaMessageReceived: (mediaMessage) => {
                        console.log(
                          "Media message received successfully",
                          mediaMessage
                        );
                        // Handle media message
                        addMessageNotification(mediaMessage);
                      },
                      onCustomMessageReceived: (customMessage) => {
                        console.log(
                          "Custom message received successfully",
                          customMessage
                        );
                        // Handle custom message
                        addMessageNotification(customMessage);
                      },
                    })
                  );
                },
                inConnecting: () => {
                  console.log("ConnectionListener => In connecting");
                },
                onDisconnected: () => {
                  console.log("ConnectionListener => On Disconnected");
                },
              })
            );
          },
          (error) => {
            console.log("CometChat login failed with exception:", { error });
            // User login failed, check error and take appropriate action.
          }
        )
        .catch((error) => {
          console.log("comet chat couldn't log in!");
          console.log(error);
        });
    } else {
      console.log("bailing cometchat login.... no comet chat auth token");
    }
  },
  (error) => {
    console.log("CometChat init error! ", error);
  }
);

Hello @justinfaulkner,

We have fixed this issue in the latest SDK v2.1.1. Can you please update to the latest SDK and check if it works on your end.

Warm Regards,

CometChat Pro
Mayur Bhandari

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.