Quick Reference - Listen for incoming messages:
- Adding a listener to receive real-time messages when your app is running
- Calling a method to retrieve missed messages when your app was not running
Real-Time Messages
In other words, as a recipient, how do I receive messages when my app is running? To receive real-time incoming messages, you need to register the MessageListener wherever you wish to receive the incoming messages. You can use theaddMessageListener() method to do so.
- JavaScript
- TypeScript
Response
Response
onTextMessageReceived — Text message object received via the listener:TextMessage Object:
onMediaMessageReceived — Media message object received via the listener:MediaMessage Object:
onCustomMessageReceived — Custom message object received via the listener:CustomMessage Object:
sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:data.metadata.@injected Object:data.metadata.@injected.extensions Object:data.metadata.@injected.extensions.link-preview Object:data.moderation Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:onMediaMessageReceived — Media message object received via the listener:MediaMessage Object:
sender Object:receiver Object:data Object:data.attachments Array (per item):data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.moderation Object:onCustomMessageReceived — Custom message object received via the listener:CustomMessage Object:
sender Object:receiver Object:customData Object:data Object:data.customData Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:data.metadata.@injected Object:data.metadata.@injected.extensions Object:data.metadata.@injected.extensions.link-preview Object:data.moderation Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:- JavaScript
- TypeScript
Missed Messages
In other words, as a recipient, how do I receive messages that I missed when my app was not running? For most use cases, you will need to add functionality to load missed messages. If you’re building an on-demand or live streaming app, you may choose to skip this and fetch message history instead. Using the sameMessagesRequest class and the filters provided by the MessagesRequestBuilder class, you can fetch the messages that were sent to the logged-in user but were not delivered to them as they were offline. For this, you will require the ID of the last message received. You can either maintain it at your end or use the getLastDeliveredMessageId() method provided by the CometChat class. This ID needs to be passed to the setMessageId() method of the builder class.
Now using the fetchNext() method, you can fetch all the messages that were sent to the user when they were offline.
Calling the fetchNext() method on the same object repeatedly allows you to fetch all the offline messages for the logged-in user.
For a Particular One-on-one Conversation
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of missed message objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:data.metadata.@injected Object:data.metadata.@injected.extensions Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
For a Particular Group
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of missed group message objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object (Group):data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:data.metadata.@injected Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
Unread Messages
In other words, as a logged-in user, how do I fetch the messages I’ve not read? Using theMessagesRequest class described above, you can fetch the unread messages for the logged-in user. In order to achieve this, you need to set the unread variable in the builder to true using the setUnread() method so that only the unread messages are fetched.
For a Particular One-on-one Conversation
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of unread message objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:data.metadata.@injected Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
For a Particular Group
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of unread group message objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object (Group):data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
Base Message: The list of messages received is in the form of objects of
BaseMessage class. 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.Message History
In other words, as a logged-in user, how do I fetch the complete message history?For a Particular One-on-one Conversation
Using theMessagesRequest class and the filters for the MessagesRequestBuilder class as shown in the below code snippet, you can fetch the entire conversation between the logged-in user and any particular user. For this use case, it is mandatory to set the UID parameter using the setUID() method of the builder. This UID is the unique ID of the user for which the conversation needs to be fetched.
- JavaScript
- TypeScript
fetchPrevious() method on the same object repeatedly allows you to fetch the entire conversation between the logged-in user and the specified user. This can be implemented with upward scrolling to display the entire conversation.
Response
Response
On Success — Returns an array of message history objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
For a Particular Group
Using theMessagesRequest class and the filters for the MessagesRequestBuilder class as shown in the below code snippet, you can fetch the entire conversation for any group provided you have joined the group. For this use case, it is mandatory to set the GUID parameter using the setGUID() method of the builder. This GUID is the unique identifier of the Group for which the messages are to be fetched.
- JavaScript
- TypeScript
fetchPrevious() method on the same object repeatedly allows you to fetch the entire conversation for the group. This can be implemented with upward scrolling to display the entire conversation.
Response
Response
On Success — Returns an array of group message history objects:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object (Group):data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:metadata.@injected Object:metadata.@injected.extensions Object:metadata.@injected.extensions.link-preview Object:On Failure — Error Object:
Search Messages
In other words, as a logged-in user, how do I search a message? In order to do this, you can use thesetSearchKeyword() method. This method accepts a string as input. When set, the SDK will fetch messages which match the searchKeyword.
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 & Custom plans. 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).For a Particular One-on-one Conversation
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of messages matching the search keyword:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object:data Object:data.entities Object:metadata Object:On Failure — Error Object:
For a Particular Group
- JavaScript
- TypeScript
Response
Response
On Success — Returns an array of group messages matching the search keyword:Message Object (per item in array):
On Failure — Error Object:
sender Object:receiver Object (Group):data Object:metadata Object:On Failure — Error Object:
Unread Message Count
In other words, as a logged-in user, how do I find out the number of unread messages that I have?For a Particular One-on-one Conversation
How do I find out the number of unread messages I have from a particular user? In order to get the unread message count for a particular user, you can use thegetUnreadMessageCountForUser() method.
This method has the below two variants:
- JavaScript
- TypeScript
- JavaScript
- TypeScript
- JavaScript
- TypeScript
Response
Response
On Success — Returns an object with UID as key and unread count as value:Unread Count Object:
On Failure — Error Object:
Example:
{"cometchat-uid-3": 2}On Failure — Error Object:
For a Particular Group Conversation
How do I find out the number of unread messages I have in a single group? In order to get the unread message count for a particular group, you can use thegetUnreadMessageCountForGroup() method.
This method has the below two variants:
- JavaScript
- TypeScript
- JavaScript
- TypeScript
- JavaScript
- TypeScript
Response
Response
On Success — Returns an object with GUID as key and unread count as value:Unread Count Object:
On Failure — Error Object:
Example:
{"group_1748415903905": 3}On Failure — Error Object:
For All One-on-one & Group Conversations
How do I find out the number of unread messages for every one-on-one and group conversation? In order to get all the unread message count combined i.e unread message counts for all the users and groups, you can use thegetUnreadMessageCount() method.
This method has two variants:
- JavaScript
- TypeScript
- JavaScript
- TypeScript
- JavaScript
- TypeScript
users- The value for this key holds another object that holds the UID as key and their corresponding unread message count as value.groups- The value for this key holds another object that holds the GUID as key and their corresponding unread message count as value.
Response
Response
On Success — Returns an object with users and groups unread counts:Unread Count Object:
On Failure — Error Object:
users Object:Example:
{"cometchat-uid-3": 2}groups Object:Example:
{"group_1748415903905": 2}On Failure — Error Object:
For All One-on-one Conversations
In order to fetch the unread message counts for just the users, you can use thegetUnreadMessageCountForAllUsers() method. This method just like others has two variants:
- JavaScript
- TypeScript
- JavaScript
- TypeScript
- JavaScript
- TypeScript
Response
Response
On Success — Returns an object with UIDs as keys and unread counts as values:Unread Count Object:
On Failure — Error Object:
Example:
{"cometchat-uid-3": 2, "cometchat-uid-4": 3}On Failure — Error Object:
For All Group Conversations
In order to fetch the unread message counts for just the groups, you can use thegetUnreadMessageCountForAllGroups() method. This method just like others has two variants:
- JavaScript
- TypeScript
- JavaScript
- TypeScript
- JavaScript
- TypeScript
Response
Response
On Success — Returns an object with GUIDs as keys and unread counts as values:Unread Count Object:
On Failure — Error Object:
Example:
{"group_1748415903905": 2, "group_1762515421478": 2, "group_1762515423434": 5}On Failure — Error Object:
Best Practices
Best Practices
- Always remove message listeners when a component unmounts to prevent memory leaks
- Use unique, descriptive listener IDs (e.g.,
"chat-screen-listener") to avoid conflicts - Use
getLastDeliveredMessageId()to efficiently fetch only missed messages - Implement pagination with
fetchPrevious()for message history to avoid loading too many messages at once - Use
setUnread(true)to fetch only unread messages when building notification badges
Troubleshooting
Troubleshooting
- Not receiving messages: Ensure the message listener is registered and the user is logged in
- Duplicate messages: Check that you’re not adding the same listener ID multiple times
- Missing messages after reconnect: Use the missed messages pattern with
getLastDeliveredMessageId()to catch up - Unread count not updating: Ensure you’re calling the unread count methods after marking messages as read
Next Steps
Send a Message
Send text, media, and custom messages to users and groups
Delivery & Read Receipts
Track when messages are delivered and read
Typing Indicators
Show real-time typing status in conversations
Message Structure
Understand message categories, types, and hierarchy