connect static method

Future<Null> connect({
  1. dynamic onSuccess(
    1. String result
    )?,
  2. dynamic onError(
    1. CometChatException excep
    )?,
})

will establish the Web-socket connections manually

While calling the init() function on the app startup, need to inform the SDK that you will be managing the web socket connect from autoEstablishSocketConnection.

Once the connection is established, you will start receiving all the real-time events for the logged in user

Migration Note: Migrated from platform channels to native Dart implementation. Uses RealtimeRepository for WebSocket connection management. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.connect()

Implementation

static Future<Null> connect(
    {Function(String successMessage)? onSuccess,
    Function(CometChatException e)? onError}) async {
  // ENG-35438 / Layer 1: short-circuit synchronously when the WS-block
  // flag is set (either from the Settings API parameter or from a prior
  // server-side ERR_CONNECTION_NOT_ALLOWED rejection). No network call.
  if (WsBlockState.isBlocked) {
    Logger.warning(
        'CometChat', 'connect() blocked: ${ErrorCodes.errConnectionNotAllowedMessage}');
    if (onError != null) {
      onError(CometChatException(
        ErrorCodes.errConnectionNotAllowed,
        ErrorCodes.errConnectionNotAllowedMessage,
        ErrorCodes.errConnectionNotAllowedMessage,
      ));
    }
    return;
  }
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call realtime repository to establish WebSocket
    await sdk.realtime.connect();

    // Call success callback
    final result = 'WebSocket connection established';
    if (onSuccess != null) onSuccess(result);
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
}