Skip to main content
FieldValue
Package@cometchat/chat-uikit-react
Key componentsCometChatSearchMessages, CometChatMessageList, CometChatMessageComposer, CometChatMessageHeader
InitCometChatUIKit.init(UIKitSettings) then CometChatUIKit.login("UID")
PurposeFull-text message search across conversations with result routing and navigation
Sample appGitHub
RelatedAll Guides
Search Messages lets users locate specific messages across conversations and groups using keyword search, then navigate directly to the result. Before starting, complete the Getting Started guide for your framework.

Components

Component / ClassRole
CometChatSearchMessagesMain container for searching messages
CometChatMessageListDisplays filtered messages based on search results
CometChatMessageComposerSupports navigation after selecting a search result
CometChatMessageHeaderDisplays search context and navigation controls

Integration Steps

1. Search State Management

Track the current search keyword and the message ID to scroll to. When a new search is triggered, reset goToMessageId so the list doesn’t jump to a stale result. File: CometChatHome.tsx
const [searchKeyword, setSearchKeyword] = useState<string>("");
const [goToMessageId, setGoToMessageId] = useState<number | undefined>(undefined);

const onSearch = (keyword: string) => {
    setSearchKeyword(keyword);
    setGoToMessageId(undefined);
};

2. Search Input

Render the search component and wire its onSearch callback to update the keyword state. The placeholder text is localized. File: CometChatHome.tsx
<CometChatSearchMessages
    onSearch={(keyword) => onSearch(keyword)}
    placeholder={getLocalizedString("search_messages_placeholder")}
/>

3. Search Result Integration

Pass searchKeyword and goToMessageId to CometChatMessageList. The list filters messages matching the keyword and highlights them. When goToMessageId is set, the list scrolls to that message. File: CometChatHome.tsx
<CometChatMessageList
    searchKeyword={searchKeyword}
    goToMessageId={goToMessageId}
    user={selectedUser}
    group={selectedGroup}
/>

4. Navigate to Search Result

When a user taps a search result, set goToMessageId to that message’s ID. The message list will scroll to and highlight the target message. File: CometChatHome.tsx
const handleResultClick = (messageId: number) => {
    setGoToMessageId(messageId);
};
Attach handleResultClick to your message search result selection logic.

Feature Matrix

FeatureComponent / MethodFile
Search inputCometChatSearchMessagesCometChatHome.tsx
Display resultsCometChatMessageListCometChatHome.tsx
Keyword highlightingsearchKeyword propCometChatHome.tsx
Navigate to resultgoToMessageIdCometChatHome.tsx
State managementonSearch handlerCometChatHome.tsx

Next Steps