
Alt text: Screenshot of a working CometChat-powered React app with a user logged in and active chat visible
You’ll build a simple React application that allows users log in, view conversations, and exchange real-time text messages using CometChat’s UI Kit.
In this Section, you’ll learn how to integrate CometChat into a React application using the official CometChat UI Kit.
Real-time chat is a foundational feature in many modern apps, from customer support to social platforms. CometChat offers a robust SDK and UI Kit that makes it fast and reliable to add chat functionality without building everything from scratch.
Knowledge Required
- Basic React hooks
- React component structure (functional components, JSX)
- ES6+ JavaScript
Tools Required
- Node.js v16+
- npm (v8+)
- Git (optional, for cloning the repository)
- A modern code editor (e.g., VS Code)
Create a new React project using Create React App with the TypeScript template:
npm create vite@latest cometchat-react-ts-app --template react-ts
cd cometchat-react-ts-app
To integrate CometChat into your project, install the required SDK and UI Kit using:
npm install @cometchat/chat-sdk-javascript @cometchat/chat-uikit-react- The file initializes CometChat using TypeScript.
- It imports CometChat from the SDK, builds
AppSettingswith presence subscription, and callsCometChat.init(). - Logs success or error messages to the console for debugging.
- The function
initializeCometChat()is called before rendering theAppcomponent. - This ensures CometChat is fully initialized before any UI elements load, preventing runtime issues.
With this setup, CometChat is properly integrated and ready for real-time communication.
Login.tsx– Handles user authentication viaCometChat.login(userID).CometChatSelector.tsx– Tracks logged-in users, displays conversations, and updates selected chat items.App.tsx– Manages state, handles conversation selection, and renders chat interface components.
This setup ensures a seamless chat experience.
- Start the development server:
npm run dev
- Open http://localhost:3000 in your browser.
- Log in, select a conversation, and send messages in real-time. Troubleshooting- Ensure .env values (APP_ID, REGION, AUTH_KEY) are correct.
- Verify at least one user exists in the CometChat Dashboard.
- Confirm the recipient user is online.
Your React TypeScript app now integrates CometChat UI Kit, enabling users to log in, view conversations, and send messages.Next Steps- Enable image and file sharing.
- Store message history locally using IndexedDB.
- Add typing indicators and read receipts.
- Expand to voice/video calls using CometChat’s SDK