The One-to-One Chat feature provides a streamlined direct messaging interface, making it ideal for support chats, dating apps, and private messaging platforms. This setup eliminates distractions by focusing solely on a dedicated chat window.
Tip: You can fork the sandbox, insert your CometChat credentials (App ID, Region, Auth Key.) in the code, and immediately preview how the UI and messages respond in real time.
import { useEffect, useState } from "react";import { CometChatMessageComposer, CometChatMessageHeader, CometChatMessageList } from "@cometchat/chat-uikit-react";import { CometChat } from "@cometchat/chat-sdk-javascript";import "./App.css";import '@cometchat/chat-uikit-react/css-variables.css';function App() { const [selectedUser, setSelectedUser] = useState<CometChat.User | undefined>(undefined); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [selectedGroup, setSelectedGroup] = useState<CometChat.Group | undefined>(undefined); useEffect(() => { // Fetch user or group from CometChat SDK whose chat you want to load. /** Fetching User */ const UID = "cometchat-uid-1"; CometChat.getUser(UID).then( user => { setSelectedUser(user); }, error => { console.log("User fetching failed with error:", error); } ); /** Fetching Group */ // const GUID = "GUID" // CometChat.getGroup(GUID).then( // group => { // setSelectedGroup(group); // }, error => { // console.log("User fetching failed with error:", error); // } // ); }, []); return ( <> {selectedUser || selectedGroup ? ( <div className="messages-wrapper"> <CometChatMessageHeader user={selectedUser} group={selectedGroup} /> <CometChatMessageList user={selectedUser} group={selectedGroup} /> <CometChatMessageComposer user={selectedUser} group={selectedGroup} /> </div> ) : ( <div className="empty-conversation">Please set a user or group in App.tsx.</div> )} </> );};export default App;
In the code snippet above, ensure you select either a user or a group based on your chat requirement. You can also determine this dynamically depending on the conversation type.