Receive real-time messages, fetch missed and unread messages, retrieve message history, search messages, and get unread counts using the CometChat JavaScript SDK.
Register the MessageListener to receive incoming messages in real-time using addMessageListener().
Always remove listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
Message Listener
TypeScript
Report incorrect code
Copy
Ask AI
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); }, }));
Report incorrect code
Copy
Ask AI
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); }, }));
Parameter
Description
listenerID
An ID that uniquely identifies that listener
Remove the listener when you no longer need to receive messages:
Remove Message Listener
TypeScript
Report incorrect code
Copy
Ask AI
let listenerID = "UNIQUE_LISTENER_ID";CometChat.removeMessageListener(listenerID);
Report incorrect code
Copy
Ask AI
let listenerID: string = "UNIQUE_LISTENER_ID";CometChat.removeMessageListener(listenerID);
As a sender, you will not receive your own message in a real-time message event. However, if a user is logged-in using multiple devices, they will receive an event for their own message in other devices.
Each listener callback receives the specific message subclass — TextMessage, MediaMessage, or CustomMessage — depending on the message type. Access the data using getter methods:
Fetch messages that were sent while your app was offline using MessagesRequestBuilder with setMessageId() and fetchNext().Use getLastDeliveredMessageId() to get the ID of the last delivered message, then fetch all messages after that point. Call fetchNext() repeatedly on the same object to paginate through all missed messages.
Fetch Missed Messages of a particular group conversation
Group Conversation
TypeScript
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let latestId = await CometChat.getLastDeliveredMessageId();let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setMessageId(latestId) .setLimit(limit) .build();messagesRequest.fetchNext().then( (messages) => { console.log("Message list fetched:", messages); }, (error) => { console.log("Message fetching failed with error:", error); });
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, latestId: number = await CometChat.getLastDeliveredMessageId(), messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setMessageId(latestId) .setLimit(limit) .build();messagesRequest.fetchNext().then( (messages: CometChat.BaseMessage[]) => { console.log("Message list fetched:", messages); }, (error: CometChat.CometChatException) => { console.log("Message fetching failed with error:", error); });
The fetchNext() method returns an array of BaseMessage objects (which may be TextMessage, MediaMessage, or other subclasses). Access the data using getter methods:
Fetch Unread Messages of a particular group conversation
Group Conversation
TypeScript
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUnread(true) .setLimit(limit) .build();messagesRequest.fetchPrevious().then( (messages) => { console.log("Message list fetched:", messages); }, (error) => { console.log("Message fetching failed with error:", error); });
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUnread(true) .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); });
The list of messages received is in the form of BaseMessage objects. A BaseMessage can either be an object of the TextMessage, MediaMessage, CustomMessage, Action or Call class. You can use the instanceof operator to check the type of object.
Fetch the complete conversation history using MessagesRequestBuilder with fetchPrevious(). Call fetchPrevious() repeatedly on the same object to paginate through the entire conversation — useful for implementing upward scrolling.
Fetch Message History of a particular group conversation
Set the GUID using setGUID() to fetch messages from a group you’ve joined.
Group Conversation
TypeScript
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .build();messagesRequest.fetchPrevious().then( (messages) => { console.log("Message list fetched:", messages); }, (error) => { console.log("Message fetching failed with error:", error); });
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, 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); });
The fetchPrevious() method returns an array of BaseMessage objects (which may be TextMessage, MediaMessage, or other subclasses). Access the data using getter methods:
Use setSearchKeyword() to search for messages matching a keyword.
By default, the searchKey is searched only in message text. However, if you enable Conversation & Advanced Search, the searchKey will be searched in message text, file name, mentions & mime type of the file.The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)