CometChat Flutter Chat SDK

CometChat enables you to add voice, video & text chat for your website & app. This guide demonstrates how to add chat to an Flutter application using CometChat.

Prerequisites :star:

Before you begin, ensure you have met the following requirements:
✅   You have Android Studio or Xcode installed in your machine.
✅   You have a Android Device or Emulator with Android Version 5.0 or above.
✅   You have a IOS Device or Emulator with IOS 11.0 or above.
✅   You have read CometChat Key Concepts.


Installing CometChat Flutter SDK

Setup :wrench:

To setup Fluter SDK, you need to first register on CometChat Dashboard. Click here to sign up.

i. Get your Application Keys :key:

Signup for CometChat and then:

  1. Create a new app: Click Add App option available → Enter App Name & other information → Create App
  2. At the Top in QuickStart section you will find Auth Key & App ID or else you can head over to the API & Auth Keys section and note the Auth Key and App ID

ii. Add the CometChat Dependency

  • 1. To use this plugin, add cometchat as a dependency in your pubspec.yaml file.
    2. add the following code to podfile inside IOS section of your app

    post_install do |installer|
    installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)

    target.build_configurations.each do |build_configuration|
    build_configuration.build_settings['EXCLUDED_ARCHSsdk=iphonesimulator*'] = 'arm64 i386'
    build_configuration.build_settings'ENABLE_BITCODE' = 'NO' end

    end
    end

    1. For IOS change ios deployment target to 11 or higher
    2. For Ios navigate to your IOS folder in terminal or CMD and do pod install . For apple chip system use rositta terminal.
    3. To import use
    import 'package:cometchat_sdk/cometchat_sdk.dart';
    
    

Configure CometChat SDK

i. Initialize CometChat 🌟

The init() method initializes the settings required for CometChat. We suggest calling the init() method on app startup, preferably in the init() method of the Home class.

import 'package:cometchat_sdk/cometchat_sdk.dart';


String appID = "APP_ID"; // Replace with your App ID
String region = "REGION"; // Replace with your App Region ("eu" or "us")

 AppSettings appSettings = (AppSettingsBuilder()
        ..subscriptionType = CometChatSubscriptionType.allUsers
        ..region= region
        ..autoEstablishSocketConnection =  true
    ).build();

    CometChat.init(appID, appSettings, onSuccess: (String successMessage) {
      debugPrint("Initialization completed successfully  $successMessage");
    }, onError: (CometChatException e) {
      debugPrint("Initialization failed with exception: ${e.message}");
    });
:information_source:   Note - Make sure to replace region and appID with your credentials.

ii. Create User 👤

Once initialisation is successful, you will need to create a user. You need to user createUser() method to create user on the fly.

import 'package:cometchat_sdk/cometchat_sdk.dart';


String authKey = "AUTH_KEY"; // Replace with your App Auth Key
User user = User(uid: "usr1" , name: "Kevin"  );

CometChat.createUser(user,  authKey, onSuccess: (User user){
   debugPrint("Create User succesfull ${user}");

}, onError: (CometChatException e){
    debugPrint("Create User Failed with exception ${e.message}");
});

:information_source:   Note - Make sure that UID and name are specified as these are mandatory fields to create a user.

iii. Login User 👤

Once you have created the user successfully, you will need to log the user into CometChat using the login() method.

String UID = "user_id"; // Replace with the UID of the user to login
String authKey = "AUTH_KEY"; // Replace with your App Auth Key

final user = await CometChat.getLoggedInUser();
if (user == null) {
  await CometChat.login(UID, authKey,
                        onSuccess: (User user) {
                          debugPrint("Login Successful : $user" );
                        }, onError: (CometChatException e) {
                          debugPrint("Login failed with exception:  ${e.message}");
                        });
}else{
//Already logged in
}
:information_source:   Note - The login() method needs to be called only once. Also replace AUTH_KEY with your App Auth Key.

📝 Please refer to our Developer Documentation for more information on how to configure the CometChat Pro SDK and implement various features using the same.


License

This project uses the following license: LICENSE.

Libraries

models/action
models/app_entity
Builders/app_settings_request
models/attachment
Builders/banned_group_member_request
models/base_message
Builders/blocked_users_request
models/call
handlers/call_listener
main/cometchat
helpers/cometchat_helper
notification/constants/cometchat_notification_constant
notification/main/cometchat_notifications
cometchat_sdk
Exception/CometChatException
handlers/connection_listener
utils/constants
models/conversation
Builders/conversations_request
models/custom_message
notification/enums/day_of_week
notification/models/day_schedule
notification/enums/dnd_options
models/group
handlers/group_listener
models/group_member
Builders/group_members_request
notification/models/group_preferences
Builders/groups_request
models/interaction
models/interaction_goal
models/interaction_receipt
models/interactive_message
handlers/login_listener
models/media_message
notification/enums/member_actions_options
handlers/message_listener
models/message_receipt
notification/enums/messages_options
Builders/messages_request
notification/models/mute_preferences
notification/models/muted_conversation
notification/enums/muted_conversation_type
notification/models/one_on_one_preferences
notification/enums/push_platforms
notification/models/push_preferences
models/reaction
models/reaction_count
notification/models/reaction_event
Builders/reaction_request
notification/enums/reactions_options
notification/enums/replies_options
models/text_message
models/transient_message
models/typing_indicator
notification/models/unmuted_conversation
models/user
handlers/user_listener
Builders/users_request