getOnlineUserCount static method

Future<int?> getOnlineUserCount({
  1. required dynamic onSuccess(
    1. int count
    )?,
  2. required dynamic onError(
    1. CometChatException excep
    )?,
})

get the total count of online users for your app

Get the count of online users.

Migration Note: Migrated from platform channels to native Dart implementation. Uses PresenceRepository for getting online user count. Behavior and signature remain identical for backward compatibility.

Android Reference: UsersRequest.getOnlineUserCount(CallbackListener<Integer>)

method could throw PlatformException with error codes specifying the cause

Implementation

static Future<int?> getOnlineUserCount(
    {required Function(int count)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart presence repository
    final count = await sdk.presence.getOnlineUserCount();

    // Call success callback
    if (onSuccess != null) onSuccess(count);
    return count;
  } 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);
  }
  return null;
}