The CometChat UI Kit for React simplifies the process of integrating in-app chat functionality with essential messaging features. With a collection of prebuilt UI components, it offers seamless theming options, including light and dark themes, various fonts, colors, and extensive customization possibilities.Supporting both one-to-one and group conversations, the CometChat UI Kit for React empowers developers to initiate chat functionalities effortlessly. Follow the guide below to begin implementing chat features into your React applications using CometChat React UI Kit.
Before installing UI Kit for React, you need to create a CometChat application on the CometChat Dashboard, which comprises everything required in a chat service including users, groups, calls & messages. You will require the App ID, AuthKey, and Region of your CometChat application when initializing the UI Kit.i. Register on CometChat
Head over to the QuickStart or API & Auth Keys section and note the App ID, Auth Key, and Region.
Each CometChat application can be integrated with a single client app. Within the same application, users can communicate with each other across all platforms, whether they are on mobile devices or on the web.
iii. IDE Setup
You have Node.js installed on your machine.
You have a code editor like Visual Studio Code or Atom installed.
You have npm or Yarn installed.
You have a Next.js app. You can follow the Next.js Documentation to create a new app.
Create a new project by initializing a new NextJs application using Create Next App.
Navigate to your project directory and open the project in your code editor.
Begin developing your NextJs application in the “pages” directory.
Install additional dependencies as required.
2
Add Dependency
This developer kit is an add-on feature to CometChat JavaScript SDK, so installing it will also install the core Chat SDK.
Report incorrect code
Copy
Ask AI
npm install @cometchat/chat-uikit-react@5.x
3
Initialize CometChat UI Kit
The Init method initialises the settings required for CometChat. Please ensure to call this method before invoking any other methods from CometChat UI Kit or CometChat SDK.
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.
Since the CometChat JavaScript SDK requires window to function. We need to disable SSR for the UI Kit to function. Please refer to Step 5.
Report incorrect code
Copy
Ask AI
import { CometChatUIKit, 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 or leave blank if you are authenticating using Auth Token};//create the builderconst UIKitSettings: UIKitSettingsBuilder = new UIKitSettingsBuilder() .setAppId(COMETCHAT_CONSTANTS.APP_ID) .setRegion(COMETCHAT_CONSTANTS.REGION) .setAuthKey(COMETCHAT_CONSTANTS.AUTH_KEY) .subscribePresenceForAllUsers() .build();//Initialize CometChat UI KitCometChatUIKit.init(UIKitSettings) .then(() => { console.log("Initialization completed successfully"); // You can now call login function. }) .catch(console.log);
Make sure to replace the APP_ID with your CometChat appId, AUTH_KEY with your CometChat app auth key and REGION with your app region in the above code.
4
Login User
The Login method returns the User object containing all the information of the logged-in user.
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.
Since the CometChat JavaScript SDK requires window to function. We need to disable SSR for the UI Kit to function. Please refer to Step 5.
Report incorrect code
Copy
Ask AI
import { CometChatUIKit } from "@cometchat/chat-uikit-react";const UID = "UID"; //Replace with your UIDCometChatUIKit.getLoggedinUser().then((user: CometChat.User) => { if (!user) { //Login user CometChatUIKit.login(UID) .then((user: CometChat.User) => { console.log("Login Successful:", { user }); //mount your app }) .catch(console.log); } else { //mount your app }});
5
Render Conversations
Create a new file CometChatNoSSR.tsx inside the src/pages folder of your project. Here we will initialize CometChat UI Kit, login a user & then launch Conversations component.
CometChatNoSSR.tsx
Report incorrect code
Copy
Ask AI
import React, { useEffect, useState } from "react";import { CometChatUIKit, UIKitSettingsBuilder, CometChatConversations,} from "@cometchat/chat-uikit-react";import { CometChat } from "@cometchat/chat-sdk-javascript";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 or leave blank if you are authenticating using Auth Token};const CometChatNoSSR: React.FC = () => { const [user, setUser] = useState<CometChat.User | undefined>(undefined); const [selectedUser, setSelectedUser] = useState<CometChat.User | undefined>( undefined ); const [selectedGroup, setSelectedGroup] = useState< CometChat.Group | undefined >(undefined); const [activeConversation, setActiveConversation] = useState< CometChat.Conversation | undefined >(undefined); useEffect(() => { const UIKitSettings = new UIKitSettingsBuilder() .setAppId(COMETCHAT_CONSTANTS.APP_ID) .setRegion(COMETCHAT_CONSTANTS.REGION) .setAuthKey(COMETCHAT_CONSTANTS.AUTH_KEY) .subscribePresenceForAllUsers() .build(); CometChatUIKit.init(UIKitSettings) ?.then(() => { console.log("Initialization completed successfully"); CometChatUIKit.getLoggedinUser().then((loggedInUser) => { if (!loggedInUser) { CometChatUIKit.login("uid") .then((user) => { console.log("Login Successful", { user }); setUser(user); }) .catch((error) => console.error("Login failed", error)); } else { console.log("Already logged-in", { loggedInUser }); setUser(loggedInUser); } }); }) .catch((error) => console.error("Initialization failed", error)); }, []); return user ? <CometChatConversations /> : undefined;};export default CometChatNoSSR;
Disable SSR in your project for CometChatNoSSR.tsx file only. This change will be done in index.tsx file.
index.tsx
Report incorrect code
Copy
Ask AI
import { Inter } from "next/font/google";import dynamic from "next/dynamic";const inter = Inter({ subsets: ["latin"] });const CometChatComponent = dynamic(() => import("./CometChatNoSSR"), { ssr: false,});export default function Home() { return <CometChatComponent />;}
Update your global.css file in the src/styles folder to import CometChat UI Kit’s CSS file & a few cosmetic changes.
global.css
Report incorrect code
Copy
Ask AI
@import url("../../node_modules/@cometchat/chat-uikit-react/dist/styles/css-variables.css");/** Give your App a height of `100%`. Keep other CSS properties in the below selector as it is. */.root { height: 100%;}#__next { height: 100%;}html,body { height: 100%;}
6
Run the project
Report incorrect code
Copy
Ask AI
npm run dev
🎉 You can now see conversations component like below,
Step 1: Let’s create a Tabs component which will render the different tabs and on change we can render different components. Create a folder CometChatTabs in src/pages folder. And add the below code,
Step 2: Let’s create the Sidebar component which will render the different components(Conversations, Users, Groups, Calls). Create a folder CometChatSelector in src/pages folder. And add the below code,