The UI Kit’s core function is to extend the Chat SDK, essentially translating the raw data and functionality provided by the underlying methods into visually appealing and easy-to-use UI components.To effectively manage and synchronize the UI elements and data across all components in the UI Kit, we utilize internal events. These internal events enable us to keep track of changes in real time and ensure that the UI reflects the most current state of data.The CometChat UI Kit has thoughtfully encapsulated the critical Chat SDK methods within its wrapper to efficiently manage internal eventing. This layer of abstraction simplifies interaction with the underlying CometChat SDK, making it more user-friendly for developers.
You must call CometChatUIKit.init(UIKitSettings) before rendering any UI Kit components or calling any SDK methods. Initialization must complete before login.
Auth Key is for development/testing only. In production, generate Auth Tokens on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code.
This method initializes the settings required for CometChat JavaScript SDK. First, ensure UIKitSettings is set and then call the init() method on the app startup.
Make sure you replace the APP_ID, REGION and AUTH_KEY with your CometChat App ID, Region and Auth Key in the below code. The Auth Key is an optional property of the UIKitSettings Class. It is intended for use primarily during proof-of-concept (POC) development or in the early stages of application development. You can use the Auth Token method to log in securely.
JavaScript
Report incorrect code
Copy
Ask AI
import { UIKitSettingsBuilder } from "@cometchat/uikit-shared"; //import shared packageconst COMETCHAT_CONSTANTS = { APP_ID: "APP_ID", // Replace with your App ID REGION: "REGION", // Replace with your App Region AUTH_KEY: "AUTH_KEY", // Replace with your Auth Key or leave blank if you are authenticating using Auth Token};//create the builderconst UIKitSettings = new UIKitSettingsBuilder() .setAppId(COMETCHAT_CONSTANTS.APP_ID) .setRegion(COMETCHAT_CONSTANTS.REGION) .setAuthKey(COMETCHAT_CONSTANTS.AUTH_KEY) .subscribePresenceForFriends() .build();//Initialize CometChatCometChatUIKit.init(UIKitSettings)?.then(() => { //login your user});
If you want to use session storage instead of the default local storage, you can configure it during initialization:
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
import { CometChat } from "@cometchat/chat-sdk-javascript";import { UIKitSettingsBuilder } from "@cometchat/chat-uikit-react";const COMETCHAT_CONSTANTS = { APP_ID: "APP_ID", // Replace with your App ID REGION: "REGION", // Replace with your App Region AUTH_KEY: "AUTH_KEY", // Replace with your Auth Key};const UIKitSettings = new UIKitSettingsBuilder() .setAppId(COMETCHAT_CONSTANTS.APP_ID) .setRegion(COMETCHAT_CONSTANTS.REGION) .setAuthKey(COMETCHAT_CONSTANTS.AUTH_KEY) .subscribePresenceForAllUsers() .setStorageMode(CometChat.StorageMode.SESSION) .build();//Initialize CometChat UI Kit with Session StorageCometChatUIKit.init(UIKitSettings)?.then(() => { console.log("Initialization completed successfully with session storage"); // You can now call login function. }) .catch(console.log);
Report incorrect code
Copy
Ask AI
import { CometChat } from "@cometchat/chat-sdk-javascript";import { UIKitSettingsBuilder } from "@cometchat/chat-uikit-react";const COMETCHAT_CONSTANTS = { APP_ID: "APP_ID", // Replace with your App ID REGION: "REGION", // Replace with your App Region AUTH_KEY: "AUTH_KEY", // Replace with your Auth Key};const UIKitSettings = new UIKitSettingsBuilder() .setAppId(COMETCHAT_CONSTANTS.APP_ID) .setRegion(COMETCHAT_CONSTANTS.REGION) .setAuthKey(COMETCHAT_CONSTANTS.AUTH_KEY) .subscribePresenceForAllUsers() .setStorageMode(CometChat.StorageMode.SESSION) .build();//Initialize CometChat UI Kit with Session StorageCometChatUIKit.init(UIKitSettings)?.then(() => { console.log("Initialization completed successfully with session storage"); // You can now call login function. }) .catch(console.log);
This simple authentication procedure is useful when you are creating a POC or if you are in the development phase. For production apps, we suggest you use AuthToken instead of Auth Key.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
import { CometChatUIKit } from "@cometchat/chat-uikit-react"; //import uikit packageconst UID = "UID";CometChatUIKit.getLoggedinUser() .then((user) => { if (!user) { CometChatUIKit.login(UID) .then((user) => { console.log("Login Successful:", { user }); //mount your app }) .catch(console.log); } else { //user already logged in //mount your app } }) .catch(console.log);
Report incorrect code
Copy
Ask AI
import { CometChatUIKit } from "@cometchat/chat-uikit-react"; //import uikit packageconst UID: string = "UID";CometChatUIKit.getLoggedinUser() .then((user: CometChat.User) => { if (!user) { CometChatUIKit.login(UID) .then((user: CometChat.User) => { console.log("Login Successful:", { user }); // You can now launch the component }) .catch(console.log); } else { //user already logged in //You can now launch the component } }) .catch(console.log);