SearchMessagesService manages message search state using Angular signals. It handles SDK query building with keyword, attachment-type, and user/group scoping, plus pagination for loading more results.
Import
Signals (Reactive State)
| Signal | Type | Default | Description |
|---|---|---|---|
messages | WritableSignal<CometChat.BaseMessage[]> | [] | Current message search results (newest first after reversal) |
fetchState | WritableSignal<States> | States.loaded | Current fetch state: loading, loaded, empty, or error |
hasMoreResults | WritableSignal<boolean> | false | Whether more pages of results are available |
Methods
search()
Initiates a message search. Resets state, validates criteria, builds the SDK request, and fetches the first page.| Parameter | Type | Description |
|---|---|---|
keyword | string | Search keyword |
filters | CometChatSearchFilter[] | Active filters that determine attachment type filtering |
uid | string | Optional user UID to scope search to a specific user’s messages |
guid | string | Optional group GUID to scope search to a specific group’s messages |
customBuilder | MessagesRequestBuilder | Optional custom builder that overrides the default |
alwaysShowSeeMore | boolean | When true, limits initial results to 3 (preview mode with “See more” button) |
fetchState is set to States.empty.
loadMore()
Fetches the next page of results and appends to the existing list.reset()
Resets all signals to their default values and clears the internal request reference.Request Building
The service builds aCometChat.MessagesRequest based on the search criteria:
hideDeletedMessages(true)— always excludes deleted messagessetSearchKeyword()— when keyword is non-emptysetUID()— when uid is provided (scoped to user)setGUID()— when guid is provided (scoped to group)hasLinks(true)— when theLinksfilter is activesetAttachmentTypes()— maps filters to SDK attachment types:
| Filter | SDK Attachment Type |
|---|---|
Photos | CometChat.AttachmentType.IMAGE |
Videos | CometChat.AttachmentType.VIDEO |
Documents | CometChat.AttachmentType.FILE |
Audio | CometChat.AttachmentType.AUDIO |
- Limit is 3 when
alwaysShowSeeMoreis true (preview mode), 30 otherwise - Results are reversed after fetch to show newest first
Usage
The service is typically used internally byCometChatSearchMessagesListComponent, but can be injected directly:
Differences from SearchConversationsService
| Aspect | SearchConversationsService | SearchMessagesService |
|---|---|---|
| Real-time listeners | Yes (message, user, group, call, typing, receipt) | No |
| Scoping | Keyword + filters only | Keyword + filters + uid/guid |
| Attachment filtering | N/A | Photos, Videos, Documents, Audio, Links |
| Typing indicators | Yes (typingIndicatorMap signal) | No |
| Preview mode | 3 results when no filters | 3 results when alwaysShowSeeMore is true |