AI Integration Quick Reference
AI Integration Quick Reference
- Adding a real-time listener to receive messages while your app is running
- Fetching unread, or historical messages when your app starts up or the user scrolls back
The TypeScript and JavaScript APIs are identical — the only difference is type
annotations (e.g.,
: string, : CometChat.BaseMessage[]). The Real-Time
Messages section shows both for reference. The remaining sections use
TypeScript only to keep things concise — just drop the type annotations for
plain JavaScript.Real-Time Messages
Register aMessageListener to receive incoming messages as they arrive. Each callback receives the specific message subclass — TextMessage, MediaMessage, or CustomMessage.
- TypeScript
- JavaScript
Remove the listener when you no longer need it:
MessageListener also receives onMessagesSynced(messages) after the SDK catches up on reconnect. It carries updates to already-cached messages that changed while offline: reactions, edits, deletes and receipts. Messages that arrived while offline still come one at a time through the usual onTextMessageReceived, onMediaMessageReceived and other typed callbacks, with getSource() set to SYNC. See Stay in Sync.
Unread Messages
Fetch unread messages by addingsetUnread(true) to the builder. Use fetchPrevious() to retrieve them.
- One-on-One
- Group
Results are returned as
BaseMessage
objects, which may be instances of
TextMessage,
MediaMessage,
CustomMessage, Action, or Call.
Use the instanceof operator to check the type.Message History
Fetch the full conversation history usingfetchPrevious(). Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling.
- One-on-One
- Group
fetchPrevious() method returns an array of BaseMessage objects (which may be TextMessage, MediaMessage, or other subclasses).
Search Messages
With offline support on, this filter is answered by the server only; the cached page is empty.
setSearchKeyword() to the builder to filter messages by keyword.
- One-on-One
- Group
Search Capabilities
By default, search only matches message text. WithConversation & Advanced Search enabled, it also matches file names, mentions, and MIME types.
Conversation & Advanced Search is available on Advanced and Custom
plans. Enable it from the CometChat
Dashboard under Chats → Settings → General
Configuration.Unread Message Count
CometChat provides several methods to get unread counts at different scopes. All return aPromise that resolves with an object mapping IDs to counts.
Each method accepts an optional boolean parameter to exclude messages from blocked users.
Unread counts in large groups: Unread message counts are calculated only for groups with up to 300 members. See Properties and Constraints.
Single Conversation
- TypeScript
- JavaScript
All Conversations
- TypeScript
- JavaScript
Excluding Blocked Users
Passtrue as the last argument to any of the methods above:
- TypeScript
- JavaScript
CometChat.getUnreadMessageCountForUser(UID, true);
Returns a
BaseMessage object (which may be a TextMessage, MediaMessage, or CustomMessage).
Offline Behaviour
With offline support enabled,fetchPrevious() and fetchNext() accept a MessageFetchListener: onCachedMessages delivers a page from the on-device cache first (even when empty), then onServerMessages delivers the reconciled server copy. The cache answers a page only when every filter on the request is locally computable; a single server-only filter makes the cached page empty with no error, while the server phase still answers in full. Offline, the fetch ends with onError with code FAILED_TO_FETCH after the cached page. getMessageDetails() accepts a MessageDetailsListener and delivers a cached copy only when the message is cached. See Show Cached Data and the filter table.
Next Steps
Delivery & Read Receipts
Track when messages are delivered and read by recipients
Typing Indicators
Show real-time typing status in conversations