- 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:
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
AddsetSearchKeyword() 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.
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).
Next Steps
Delivery & Read Receipts
Track when messages are delivered and read by recipients
Typing Indicators
Show real-time typing status in conversations