Skip to main content
Receiving messages with CometChat has two parts:
  1. Adding a real-time listener to receive messages while your app is running
  2. 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 a MessageListener to receive incoming messages as they arrive. Each callback receives the specific message subclass — TextMessage, MediaMessage, or CustomMessage.
Remove the listener when you no longer need it:
Always remove listeners when they’re no longer needed (e.g., on component unmount). Failing to do so can cause memory leaks and duplicate event handling.
As a sender, you will not receive your own message in a real-time event. However, if a user is logged in on multiple devices, they will receive the event on the other devices.
With offline support enabled, a registered 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 adding setUnread(true) to the builder. Use fetchPrevious() to retrieve them.
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 using fetchPrevious(). Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling.
The 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.
Add setSearchKeyword() to the builder to filter messages by keyword.

Search Capabilities

By default, search only matches message text. With Conversation & 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 a Promise 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

All Conversations

Excluding Blocked Users

Pass true as the last argument to any of the methods above:
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