Overview
The CometChatSearch component provides a unified search experience across conversations and messages. It combines a debounced search input, a filter chip bar, and two lazy-loaded result sections (conversations and messages) into a single, cohesive UI.
The component follows a service-driven architecture:
- SearchConversationsService handles conversation search queries, pagination, and real-time listener updates
- SearchMessagesService handles message search queries, pagination, and attachment-type filtering
- CometChatTemplatesService provides template resolution with a priority chain:
@Input → service template → shared template → default
- Component @Input properties allow per-instance overrides for scoping, filtering, and display
Key Features
- Cross-Entity Search: Search across both conversations and messages simultaneously, or scope to one
- 9 Filter Types: Audio, Documents, Groups, Photos, Videos, Links, Unread, Conversations, Messages
- Scoped Search: Narrow results to a specific user (
uid) or group (guid)
- Debounced Input: 500ms debounce prevents excessive SDK calls while typing
- Lazy-Loaded Sections: Conversation and message result lists use
@defer for optimal initial load
- Unified State Views: Single empty/error view when both sections report the same state
- Template Customization: Override any section of conversation or message result items
- Real-Time Updates: Conversation results update with new messages, typing indicators, and user status changes
- Filter Chip Logic: Intelligent mutual exclusivity — content filters (Photos, Videos, etc.) are mutually exclusive with Links; conversation filters (Unread, Groups) can coexist
Architecture
Basic Usage
Simple Implementation
Scoped Search (Within a User or Group)
When uid or guid is set, conversation filters (Unread, Groups, Conversations) are hidden automatically and only message results are shown.
Scoped to One Entity Type
Properties
Configuration Properties
| Property | Type | Default | Description |
|---|
searchIn | CometChatSearchScope[] | [] (both) | Scopes to search in. Empty array means both Conversations and Messages |
searchFilters | CometChatSearchFilter[] | [Audio, Documents, Groups, Photos, Videos, Links, Unread] | Filter chips to display in the filter bar |
initialSearchFilter | CometChatSearchFilter | undefined | Filter that should be active by default when the component loads |
defaultSearchText | string | undefined | Pre-fill the search input and immediately trigger a search on load |
conversationsRequestBuilder | ConversationsRequestBuilder | undefined | Custom request builder for conversation search queries |
messagesRequestBuilder | MessagesRequestBuilder | undefined | Custom request builder for message search queries |
conversationOptions | (conv: Conversation) => CometChatOption[] | undefined | Function to provide custom context menu options for conversation results |
textFormatters | CometChatTextFormatter[] | [] | Custom text formatters for message content display |
messageSentAtDateTimeFormat | CalendarObject | undefined | Custom date/time format for message timestamps |
Display Control Properties
| Property | Type | Default | Description |
|---|
hideBackButton | boolean | false | Hide the back navigation button in the header |
hideGroupType | boolean | false | Hide group type icons (public, private, password) in conversation results |
hideUserStatus | boolean | false | Hide user online/offline status indicator in conversation results |
hideReceipts | boolean | false | Hide message read receipts (sent, delivered, read) in conversation results |
Scoping Properties
| Property | Type | Default | Description |
|---|
uid | string | undefined | Scope search to messages with a specific user. Hides conversation results and conversation filters |
guid | string | undefined | Scope search to messages within a specific group. Hides conversation results and conversation filters |
Template Properties
Conversation Result Templates
| Property | Type | Context | Description |
|---|
conversationItemView | TemplateRef | { $implicit: Conversation } | Custom template for the entire conversation result item |
conversationLeadingView | TemplateRef | { $implicit: Conversation } | Custom template for the leading section (avatar area) |
conversationTitleView | TemplateRef | { $implicit: Conversation } | Custom template for the title section |
conversationSubtitleView | TemplateRef | { $implicit: Conversation } | Custom template for the subtitle section (last message preview) |
conversationTrailingView | TemplateRef | { $implicit: Conversation } | Custom template for the trailing section (timestamp, badges) |
Message Result Templates
| Property | Type | Context | Description |
|---|
messageItemView | TemplateRef | { $implicit: BaseMessage } | Custom template for the entire message result item |
messageLeadingView | TemplateRef | { $implicit: BaseMessage } | Custom template for the leading section (type-specific icon) |
messageTitleView | TemplateRef | { $implicit: BaseMessage } | Custom template for the title section (conversation/sender name) |
messageSubtitleView | TemplateRef | { $implicit: BaseMessage } | Custom template for the subtitle section (message content) |
messageTrailingView | TemplateRef | { $implicit: BaseMessage } | Custom template for the trailing section (timestamp, image/video thumbnail) |
State View Templates
| Property | Type | Description |
|---|
initialView | TemplateRef | Shown before any search is performed (no keyword, no filters) |
loadingView | TemplateRef | Shown while search results are being fetched (shimmer placeholders by default) |
emptyView | TemplateRef | Shown when search returns no results |
errorView | TemplateRef | Shown when a search operation fails |
Template resolution follows a priority chain: @Input template → CometChatTemplatesService search template → CometChatTemplatesService shared template → built-in default.
Events
| Event | Payload Type | Description |
|---|
backClick | void | Emitted when the back button is clicked |
conversationClick | SearchConversationClickEvent | Emitted when a conversation result is clicked. Payload includes conversation and searchKeyword |
messageClick | SearchMessageClickEvent | Emitted when a message result is clicked. Payload includes message and searchKeyword |
searchError | CometChat.CometChatException | Emitted when a search operation fails in either section |
Event Payload Types
Filter System
The search component includes a filter chip bar that allows users to narrow results by type. Filters are organized into two categories:
Conversation Filters
These filters affect the conversation results section:
| Filter | Enum Value | Behavior |
|---|
| Unread | CometChatSearchFilter.Unread | Shows only conversations with unread messages |
| Groups | CometChatSearchFilter.Groups | Shows only group conversations |
| Conversations | CometChatSearchFilter.Conversations | Shows all conversations (explicit scope) |
Message Filters
These filters affect the message results section:
| Filter | Enum Value | Behavior |
|---|
| Photos | CometChatSearchFilter.Photos | Shows only image messages |
| Videos | CometChatSearchFilter.Videos | Shows only video messages |
| Documents | CometChatSearchFilter.Documents | Shows only file/document messages |
| Audio | CometChatSearchFilter.Audio | Shows only audio messages |
| Links | CometChatSearchFilter.Links | Shows only messages containing links |
| Messages | CometChatSearchFilter.Messages | Shows all message types (explicit scope) |
Filter Behavior Rules
- When a conversation filter is active, only the conversations section renders
- When a message filter is active, only the messages section renders
- Content filters (Photos, Videos, Documents, Audio) are mutually exclusive with Links
- Multiple conversation filters can coexist (e.g., Unread + Groups)
- When no filters are active and a keyword is entered, both sections render
- When
uid or guid is set, conversation filters are hidden automatically
Limiting Available Filters
Pre-Selecting a Filter
Unified State Management
When both conversation and message sections are active, the component coordinates their empty/error states to avoid duplicate views:
| Conversations State | Messages State | Result |
|---|
| Empty | Empty | Single unified empty view (no section headers) |
| Empty | Loaded | Conversations section hidden entirely; messages shown normally |
| Loaded | Empty | Messages section hidden entirely; conversations shown normally |
| Error | Error | Single unified error view (no section headers) |
| Error | Loaded | Conversations shows error with header; messages shown normally |
| Loaded | Error | Conversations shown normally; messages shows error with header |
When only one scope is active (via searchIn, uid, or guid), that section handles its own empty/error states independently.
Advanced Usage
Custom Request Builders
Override the default SDK query builders for conversations and messages:
Handling Search Results for Navigation
A common pattern is navigating to the matched message within its conversation when a message result is clicked:
Customization with Templates
Custom Message Result Item
Override the entire message result row:
Custom Empty and Error States
Custom Initial View
Replace the default “Search for messages, conversations…” placeholder:
Keyboard Accessibility
CometChatSearch provides keyboard accessibility for the search input, filter bar, and result items.
Keyboard Shortcuts
| Key | Action | Context |
|---|
Tab | Navigate between search input, filter chips, and result items | Global |
Shift + Tab | Navigate backwards | Global |
Enter | Activate focused result item or filter chip | When focused |
Space | Toggle filter chip or activate result item | When focused |
Escape | Clear search text and active filters | When search input is focused |
Accessibility Features
ARIA Attributes:
role="search" on the main container
role="searchbox" on the search input
role="toolbar" on the filter bar
aria-pressed on filter chips indicating active state
aria-label on back button, search input, and clear button
aria-live="assertive" on empty and error state regions
role="list" and role="listitem" on result sections
Screen Reader Support:
- Filter chip labels are localized via the translate pipe
- State changes (empty, error) are announced via
aria-live regions
- Back button and clear button have descriptive
aria-label attributes
Focus Management:
- Search input auto-focuses on component load
- Clear button restores focus to search input after clearing
- Result items are focusable with
tabindex="0" (message results)
- Visible focus indicators meeting WCAG contrast requirements
Styling with CSS Variables
The component uses CometChat CSS variables for theming. Key variables:
Filter Chip Styling
Active filter chips use a dark pill style with white text and icons:
Error Handling
Built-in Error Handling
The component handles errors from both conversation and message search services:
Error State Behavior
- If only one section errors while the other has results, the errored section shows its own error view with its section header
- If both sections error, a single unified error view is shown without section headers
- Custom
errorView templates are used when provided
Complete Example
- CometChatConversations: Full conversation list component (CometChatSearch uses
CometChatConversationItem internally for conversation results)
- CometChatSearchBar: Standalone search bar component (CometChatSearch has its own built-in search input)
- CometChatMessageList: Message list component for displaying messages in a conversation
- CometChatMessageHeader: Header component that pairs with search for navigation context
Technical Details
- Standalone Component: Can be imported and used independently
- Change Detection: Uses
OnPush strategy for optimal performance
- Lazy Loading: Child result components use
@defer blocks for code splitting
- Signal-Based State: Services use Angular signals for reactive state management
- Debounced Search: 500ms debounce on the search input to reduce SDK calls
- BEM CSS: Follows Block Element Modifier naming convention (
cometchat-search__*)
- Localization: All text uses the
translate pipe for i18n support