Receive real-time messages, fetch unread messages, retrieve message history, search messages, and get unread counts using the CometChat JavaScript SDK.
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.
Register a MessageListener to receive incoming messages as they arrive. Each callback receives the specific message subclass — TextMessage, MediaMessage, or CustomMessage.
TypeScript
JavaScript
let listenerID: string = "UNIQUE_LISTENER_ID";CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ onTextMessageReceived: (textMessage: CometChat.TextMessage) => { console.log("Text message received successfully", textMessage); }, onMediaMessageReceived: (mediaMessage: CometChat.MediaMessage) => { console.log("Media message received successfully", mediaMessage); }, onCustomMessageReceived: (customMessage: CometChat.CustomMessage) => { console.log("Custom message received successfully", customMessage); }, }));
let listenerID = "UNIQUE_LISTENER_ID";CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ onTextMessageReceived: (textMessage) => { console.log("Text message received successfully", textMessage); }, onMediaMessageReceived: (mediaMessage) => { console.log("Media message received successfully", mediaMessage); }, onCustomMessageReceived: (customMessage) => { console.log("Custom message received successfully", customMessage); }, }));
Parameter
Description
listenerID
An ID that uniquely identifies that listener. Use the same ID to remove it later.
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.
Fetch the full conversation history using fetchPrevious(). Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling.
One-on-One
Group
let UID: string = "UID";let limit: number = 30;let messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .build();messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { console.log("Message list fetched:", messages); }, (error: CometChat.CometChatException) => { console.log("Message fetching failed with error:", error); });
let GUID: string = "GUID";let limit: number = 30;let messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .build();messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { console.log("Message list fetched:", messages); }, (error: CometChat.CometChatException) => { console.log("Message fetching failed with error:", error); });
By default, search only matches message text. With Conversation & Advanced Search enabled, it also matches file names, mentions, and MIME types.
Feature
Basic Search
Advanced Search
Text search
✅
✅
File name search
❌
✅
Mentions search
❌
✅
MIME type search
❌
✅
Conversation & Advanced Search is available on Advanced and Customplans. Enable it from the CometChat Dashboard under Chats → Settings → General Configuration.
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.