Quick Reference - Listen for incoming messages:CometChat . addMessageListener ( "listener-id" , new CometChat . MessageListener ({
onTextMessageReceived : ( msg ) => console . log ( "Text:" , msg ),
onMediaMessageReceived : ( msg ) => console . log ( "Media:" , msg ),
onCustomMessageReceived : ( msg ) => console . log ( "Custom:" , msg ),
}));
// Don't forget to remove the listener when done
CometChat . removeMessageListener ( "listener-id" );
Receiving messages with CometChat has two parts:
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 the addMessageListener() method to do so.
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 );
},
})
);
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.
onTextMessageReceived — Text message object received via the listener:TextMessage Object: Parameter Type Description Sample Value idstring Unique message identifier "25195"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Message type "text"categorystring Message category "message"textstring Message text content "Hello"sentAtnumber Timestamp when sent 1771386431updatedAtnumber Timestamp when updated 1771386431senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386392hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386383hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value textstring Message text "Hello"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓ moderationobject Moderation status See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386392tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386383conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
data.metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
data.metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
data.metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
data.moderation Object:Parameter Type Description Sample Value statusstring Moderation status "approved"
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
onMediaMessageReceived — Media message object received via the listener:MediaMessage Object: Parameter Type Description Sample Value idstring Unique message identifier "25196"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Media type "image"categorystring Message category "message"sentAtnumber Timestamp when sent 1771386517updatedAtnumber Timestamp when updated 1771386517senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771386507hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771386436hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value typestring Media type "image"categorystring Message category "message"urlstring Media file URL "https://data-in.cometchat.io/..."resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."attachmentsarray Media attachments See below ↓ entitiesobject Sender and receiver entities See below ↓ moderationobject Moderation status See below ↓
data.attachments Array (per item):Parameter Type Description Sample Value urlstring File URL "https://data-in.cometchat.io/..."namestring File name "photo.jpg"mimeTypestring MIME type "image/jpeg"extensionstring File extension "jpg"sizenumber File size in bytes 142099
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771386507tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771386436conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.moderation Object:Parameter Type Description Sample Value statusstring Moderation status "approved"
onCustomMessageReceived — Custom message object received via the listener:CustomMessage Object: Parameter Type Description Sample Value idstring Unique message identifier "25197"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Custom message type "test-custom"categorystring Message category "custom"customDataobject Custom data payload See below ↓ sentAtnumber Timestamp when sent 1771386648updatedAtnumber Timestamp when updated 1771386648senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386640hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386630hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
customData Object:Parameter Type Description Sample Value greetingstring Custom greeting text "Hello from custom message!"timestampnumber Custom timestamp 1771386646780
data Object:Parameter Type Description Sample Value textstring Display text "Sent a custom message"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."customDataobject Custom data payload See below ↓ entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓ moderationobject Moderation status See below ↓
data.customData Object:Parameter Type Description Sample Value greetingstring Custom greeting text "Hello from custom message!"timestampnumber Custom timestamp 1771386646780
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386640tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771386630conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
data.metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
data.metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
data.metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
data.moderation Object:Parameter Type Description Sample Value statusstring Moderation status "approved"
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
Listener Cleanup: Always remove the listener once you don’t need to receive messages for a particular listener. Remove the listener in componentWillUnmount() or the cleanup function of a useEffect to prevent memory leaks.
var listenerID = "UNIQUE_LISTENER_ID" ;
CometChat . removeMessageListener ( listenerID );
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.
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 same MessagesRequest 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
let UID = "UID" ;
let limit = 30 ;
let latestId = await CometChat . getLastDeliveredMessageId ();
var messagesRequest = new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. setMessageId ( latestId )
. setLimit ( limit )
. build ();
messagesRequest . fetchNext (). then (
( messages ) => {
console . log ( "Message list fetched:" , messages );
},
( error ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
let UID : string = "UID" ,
limit : number = 30 ,
latestId : number = await CometChat . getLastDeliveredMessageId (),
messagesRequest : CometChat . MessagesRequest =
new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. 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 );
}
);
On Success — Returns an array of missed message objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25211"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Message type "text"categorystring Message category "message"textstring Message text content "Missed Message"sentAtnumber Timestamp when sent 1771388912updatedAtnumber Timestamp when updated 1771388912senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771388888hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value textstring Message text "Missed Message"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771388888conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
data.metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
data.metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension {"links": []}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For a Particular Group
let GUID = "GUID" ;
let limit = 30 ;
let latestId = await CometChat . getLastDeliveredMessageId ();
var 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 );
}
);
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 );
}
);
On Success — Returns an array of missed group message objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25215"conversationIdstring Conversation identifier "group_group_1748415903905"receiverIdstring Group GUID "group_1748415903905"receiverTypestring Type of receiver "group"typestring Message type "text"categorystring Message category "message"textstring Message text content "Group Missed"sentAtnumber Timestamp when sent 1771389682updatedAtnumber Timestamp when updated 1771389682senderobject Sender user details See below ↓ receiverobject Receiver group details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object (Group):Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined trueisBannedboolean Whether current user is banned falsejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data Object:Parameter Type Description Sample Value textstring Message text "Group Missed"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "group"entityobject Group entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined truejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
data.metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data {"link-preview": {"links": []}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
Unread Messages
In other words, as a logged-in user, how do I fetch the messages I’ve not read?
Using the MessagesRequest 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
let UID = "UID" ;
let limit = 30 ;
let messagesRequest = new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. setUnread ( true )
. setLimit ( limit )
. build ();
messagesRequest . fetchPrevious (). then (
( messages ) => {
console . log ( "Message list fetched:" , messages );
},
( error ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
let UID : string = "UID" ,
limit : number = 30 ,
messagesRequest : CometChat . MessagesRequest =
new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. 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 );
}
);
On Success — Returns an array of unread message objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25216"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Message type "text"categorystring Message category "message"textstring Message text content "Unread Message"sentAtnumber Timestamp when sent 1771390060deliveredAtnumber Timestamp when delivered 1771390061updatedAtnumber Timestamp when updated 1771390061senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771389781hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value textstring Message text "Unread Message"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771389781conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
data.metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data {"link-preview": {"links": []}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For a Particular Group
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 );
}
);
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 );
}
);
On Success — Returns an array of unread group message objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25217"conversationIdstring Conversation identifier "group_group_1748415903905"receiverIdstring Group GUID "group_1748415903905"receiverTypestring Type of receiver "group"typestring Message type "text"categorystring Message category "message"textstring Message text content "Unread Group Message"sentAtnumber Timestamp when sent 1771390129updatedAtnumber Timestamp when updated 1771390129senderobject Sender user details See below ↓ receiverobject Receiver group details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object (Group):Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined trueisBannedboolean Whether current user is banned falsejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data Object:Parameter Type Description Sample Value textstring Message text "Unread Group Message"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "group"entityobject Group entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined truejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data {"extensions": {"link-preview": {"links": []}}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
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 the MessagesRequest 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.
let UID = "UID" ;
let limit = 30 ;
let messagesRequest = new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. setLimit ( limit )
. build ();
messagesRequest . fetchPrevious (). then (
( messages ) => {
console . log ( "Message list fetched:" , messages );
},
( error ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
let UID : string = "UID" ,
limit : number = 30 ,
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 );
}
);
Calling the 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.
On Success — Returns an array of message history objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25220"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Message type "text"categorystring Message category "message"textstring Message text content "Historic 1-1"sentAtnumber Timestamp when sent 1771390691updatedAtnumber Timestamp when updated 1771390691senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771389781hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value textstring Message text "Historic 1-1"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "offline"lastActiveAtnumber Last active timestamp 1771389781conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"tagsarray User tags []
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data {"extensions": {"link-preview": {"links": []}}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For a Particular Group
Using the MessagesRequest 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.
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 );
}
);
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 );
}
);
Calling the 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.
On Success — Returns an array of group message history objects:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25218"conversationIdstring Conversation identifier "group_group_1748415903905"receiverIdstring Group GUID "group_1748415903905"receiverTypestring Type of receiver "group"typestring Message type "text"categorystring Message category "message"textstring Message text content "Historic Message 1"sentAtnumber Timestamp when sent 1771390631updatedAtnumber Timestamp when updated 1771390631senderobject Sender user details See below ↓ receiverobject Receiver group details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object (Group):Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined trueisBannedboolean Whether current user is banned falsejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data Object:Parameter Type Description Sample Value textstring Message text "Historic Message 1"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata See below ↓
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper See below ↓ receiverobject Receiver entity wrapper See below ↓
data.entities.sender Object:Parameter Type Description Sample Value entityTypestring Type of entity "user"entityobject User entity details See below ↓
data.entities.sender.entity Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771388895tagsarray User tags []
data.entities.receiver Object:Parameter Type Description Sample Value entityTypestring Type of entity "group"entityobject Group entity details See below ↓
data.entities.receiver.entity Object:Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 1hasJoinedboolean Whether current user has joined truejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data.metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data {"extensions": {"link-preview": {"links": []}}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data See below ↓
metadata.@injected Object:Parameter Type Description Sample Value extensionsobject Extensions data See below ↓
metadata.@injected.extensions Object:Parameter Type Description Sample Value link-previewobject Link preview extension See below ↓
metadata.@injected.extensions.link-preview Object:Parameter Type Description Sample Value linksarray Extracted links []
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
Search Messages
In other words, as a logged-in user, how do I search a message?
In order to do this, you can use the setSearchKeyword() 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).
Feature Basic Search Advanced Search Text search ✅ ✅ File name search ❌ ✅ Mentions search ❌ ✅ Mime type search ❌ ✅
For a Particular One-on-one Conversation
let UID = "UID" ;
let limit = 30 ;
let searchKeyword = "Hello" ;
let messagesRequest = new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. setLimit ( limit )
. setSearchKeyword ( searchKeyword )
. build ();
messagesRequest . fetchPrevious (). then (
( messages ) => {
console . log ( "Message list fetched:" , messages );
},
( error ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
let UID : string = "UID" ,
limit : number = 30 ,
searchKeyword : string = "Hello" ,
messagesRequest : CometChat . MessagesRequest =
new CometChat . MessagesRequestBuilder ()
. setUID ( UID )
. setLimit ( limit )
. setSearchKeyword ( searchKeyword )
. build ();
messagesRequest . fetchPrevious (). then (
( messages : CometChat . BaseMessage []) => {
console . log ( "Message list fetched:" , messages );
},
( error : CometChat . CometChatException ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
On Success — Returns an array of messages matching the search keyword:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25224"conversationIdstring Conversation identifier "cometchat-uid-2_user_cometchat-uid-3"receiverIdstring Receiver’s UID "cometchat-uid-2"receiverTypestring Type of receiver "user"typestring Message type "text"categorystring Message category "message"textstring Message text content "xq7SearchTest"sentAtnumber Timestamp when sent 1771392432deliveredAtnumber Timestamp when delivered 1771392432readAtnumber Timestamp when read 1771392432updatedAtnumber Timestamp when updated 1771392432senderobject Sender user details See below ↓ receiverobject Receiver user details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771391162hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-2"namestring User’s display name "George Alan"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771391157hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
data Object:Parameter Type Description Sample Value textstring Message text "xq7SearchTest"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities See below ↓ metadataobject Injected metadata {"@injected": {"extensions": {"link-preview": {"links": []}}}}
data.entities Object:Parameter Type Description Sample Value senderobject Sender entity wrapper Contains entityType and entity receiverobject Receiver entity wrapper Contains entityType and entity
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data {"extensions": {"link-preview": {"links": []}}}
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For a Particular Group
let GUID = "GUID" ;
let limit = 30 ;
let searchKeyword = "Hello" ;
let messagesRequest = new CometChat . MessagesRequestBuilder ()
. setGUID ( GUID )
. setLimit ( limit )
. setSearchKeyword ( searchKeyword )
. build ();
messagesRequest . fetchPrevious (). then (
( messages ) => {
console . log ( "Message list fetched:" , messages );
},
( error ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
let GUID : string = "GUID" ,
limit : number = 30 ,
searchKeyword : string = "Hello" ,
messagesRequest : CometChat . MessagesRequest =
new CometChat . MessagesRequestBuilder ()
. setGUID ( GUID )
. setLimit ( limit )
. setSearchKeyword ( searchKeyword )
. build ();
messagesRequest . fetchPrevious (). then (
( messages : CometChat . BaseMessage []) => {
console . log ( "Message list fetched:" , messages );
},
( error : CometChat . CometChatException ) => {
console . log ( "Message fetching failed with error:" , error );
}
);
On Success — Returns an array of group messages matching the search keyword:Message Object (per item in array): Parameter Type Description Sample Value idstring Unique message identifier "25225"conversationIdstring Conversation identifier "group_group_1748415903905"receiverIdstring Group GUID "group_1748415903905"receiverTypestring Type of receiver "group"typestring Message type "text"categorystring Message category "message"textstring Message text content "xq8SearchTest"sentAtnumber Timestamp when sent 1771392513updatedAtnumber Timestamp when updated 1771392513senderobject Sender user details See below ↓ receiverobject Receiver group details See below ↓ dataobject Additional message data See below ↓ metadataobject Message metadata See below ↓ reactionsarray Message reactions []mentionedUsersarray Users mentioned in message []mentionedMeboolean Whether current user is mentioned false
sender Object:Parameter Type Description Sample Value uidstring User’s unique identifier "cometchat-uid-3"namestring User’s display name "Nancy Grace"avatarstring User’s avatar URL "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"rolestring User’s role "default"statusstring User’s online status "online"lastActiveAtnumber Last active timestamp 1771391162hasBlockedMeboolean Whether user has blocked current user falseblockedByMeboolean Whether current user blocked this user falsedeactivatedAtnumber Deactivation timestamp (0 if active) 0tagsarray User tags []
receiver Object (Group):Parameter Type Description Sample Value guidstring Group’s unique identifier "group_1748415903905"namestring Group’s display name "3 People Group"typestring Group type "public"conversationIdstring Conversation identifier "group_group_1748415903905"ownerstring Group owner UID "123456"scopestring Current user’s scope in group "admin"membersCountnumber Total members in group 12onlineMembersCountnumber Online members count 2hasJoinedboolean Whether current user has joined trueisBannedboolean Whether current user is banned falsejoinedAtnumber Timestamp when user joined 1749203133createdAtnumber Group creation timestamp 1748415957updatedAtnumber Group update timestamp 1771245340
data Object:Parameter Type Description Sample Value textstring Message text "xq8SearchTest"resourcestring SDK resource identifier "REACT_NATIVE-4_0_14-..."entitiesobject Sender and receiver entities Contains sender and receiver wrappers metadataobject Injected metadata {"@injected": {"extensions": {"link-preview": {"links": []}}}}
metadata Object:Parameter Type Description Sample Value @injectedobject Injected extensions data {"extensions": {"link-preview": {"links": []}}}
On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
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 the getUnreadMessageCountForUser() method.
This method has the below two variants:
CometChat . getUnreadMessageCountForUser ( UID );
let UID : string = "UID" ;
CometChat . getUnreadMessageCountForUser ( UID );
If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
CometChat . getUnreadMessageCountForUser ( UID , hideMessagesFromBlockedUsers );
let UID : string = "UID" ,
hideMessagesFromBlockedUsers : boolean = true ;
CometChat . getUnreadMessageCountForUser ( UID , hideMessagesFromBlockedUsers );
let UID = "UID" ;
CometChat . getUnreadMessageCountForUser ( UID ). then (
( unreadMessageCountObject ) => {
console . log ( "Unread message count fetched" , unreadMessageCountObject );
},
( error ) => {
console . log ( "Error in getting unread message count" , error );
}
);
let UID : string = "UID" ;
CometChat . getUnreadMessageCountForUser ( UID ). then (
( unreadMessageCount : Object ) => {
console . log ( "Unread message count fetched" , unreadMessageCount );
},
( error : CometChat . CometChatException ) => {
console . log ( "Error in getting unread message count" , error );
}
);
It will return an object which will contain the UID as the key and the unread message count as the value.
On Success — Returns an object with UID as key and unread count as value:Unread Count Object: Parameter Type Description Sample Value {UID}number Unread message count for the specified user 2
Example: {"cometchat-uid-3": 2} On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
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 the getUnreadMessageCountForGroup() method.
This method has the below two variants:
CometChat . getUnreadMessageCountForGroup ( GUID );
let GUID : string = "GUID" ;
CometChat . getUnreadMessageCountForGroup ( GUID );
If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
CometChat . getUnreadMessageCountForGroup ( GUID , hideMessagesFromBlockedUsers );
let GUID : string = "GUID" ,
hideMessagesFromBlockedUsers : boolean = true ;
CometChat . getUnreadMessageCountForGroup ( GUID , hideMessagesFromBlockedUsers );
let GUID = "GUID" ;
CometChat . getUnreadMessageCountForGroup ( GUID ). then (
( unreadMessageCountObject ) => {
console . log ( "Unread message count fetched" , unreadMessageCountObject );
},
( error ) => {
console . log ( "Error in getting unread message count" , error );
}
);
let GUID : string = "GUID" ;
CometChat . getUnreadMessageCountForGroup ( GUID ). then (
( unreadMessageCount : Object ) => {
console . log ( "Unread message count fetched" , unreadMessageCount );
},
( error : CometChat . CometChatException ) => {
console . log ( "Error in getting unread message count" , error );
}
);
It will return an object which will contain the GUID as the key and the unread message count as the value.
On Success — Returns an object with GUID as key and unread count as value:Unread Count Object: Parameter Type Description Sample Value {GUID}number Unread message count for the specified group 3
Example: {"group_1748415903905": 3} On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
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 the getUnreadMessageCount() method.
This method has two variants:
CometChat . getUnreadMessageCount ();
CometChat . getUnreadMessageCount ();
If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
CometChat . getUnreadMessageCount ( hideMessagesFromBlockedUsers );
let hideMessagesFromBlockedUsers : boolean = true ;
CometChat . getUnreadMessageCount ( hideMessagesFromBlockedUsers );
CometChat . getUnreadMessageCount (). then (
( unreadMessageCountObject ) => {
console . log ( "Unread message count fetched" , unreadMessageCountObject );
},
( error ) => {
console . log ( "Error in getting unread message count" , error );
}
);
CometChat . getUnreadMessageCount (). then (
( unreadMessageCount : Object ) => {
console . log ( "Unread message count fetched" , unreadMessageCount );
},
( error : CometChat . CometChatException ) => {
console . log ( "Error in getting unread message count" , error );
}
);
It returns an object having two keys:
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.
On Success — Returns an object with users and groups unread counts:Unread Count Object: Parameter Type Description Sample Value usersobject Object with UIDs as keys and unread counts as values See below ↓ groupsobject Object with GUIDs as keys and unread counts as values See below ↓
users Object:Parameter Type Description Sample Value {UID}number Unread count for each user 2
Example: {"cometchat-uid-3": 2} groups Object:Parameter Type Description Sample Value {GUID}number Unread count for each group 2
Example: {"group_1748415903905": 2} On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For All One-on-one Conversations
In order to fetch the unread message counts for just the users, you can use the getUnreadMessageCountForAllUsers() method. This method just like others has two variants:
CometChat . getUnreadMessageCountForAllUsers ();
CometChat . getUnreadMessageCountForAllUsers ();
If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
CometChat . getUnreadMessageCountForAllUsers ( hideMessagesFromBlockedUsers );
let hideMessagesFromBlockedUsers : boolean = true ;
CometChat . getUnreadMessageCountForAllUsers ( hideMessagesFromBlockedUsers );
CometChat . getUnreadMessageCountForAllUsers (). then (
( unreadMessageCountObject ) => {
console . log ( "Unread message count fetched" , unreadMessageCountObject );
},
( error ) => {
console . log ( "Error in getting unread message count" , error );
}
);
CometChat . getUnreadMessageCountForAllUsers (). then (
( unreadMessageCount : Object ) => {
console . log ( "Unread message count fetched" , unreadMessageCount );
},
( error : CometChat . CometChatException ) => {
console . log ( "Error in getting unread message count" , error );
}
);
It returns an object which will contain the UID as the key and the unread message count as the value.
On Success — Returns an object with UIDs as keys and unread counts as values:Unread Count Object: Parameter Type Description Sample Value {UID}number Unread message count for each user 2 or 3
Example: {"cometchat-uid-3": 2, "cometchat-uid-4": 3} On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
For All Group Conversations
In order to fetch the unread message counts for just the groups, you can use the getUnreadMessageCountForAllGroups() method. This method just like others has two variants:
CometChat . getUnreadMessageCountForAllGroups ();
CometChat . getUnreadMessageCountForAllGroups ();
If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
CometChat . getUnreadMessageCountForAllGroups ( hideMessagesFromBlockedUsers );
let hideMessagesFromBlockedUsers : boolean = true ;
CometChat . getUnreadMessageCountForAllGroups ( hideMessagesFromBlockedUsers );
CometChat . getUnreadMessageCountForAllGroups (). then (
( unreadMessageCountObject ) => {
console . log ( "Unread message count fetched" , unreadMessageCountObject );
},
( error ) => {
console . log ( "Error in getting unread message count" , error );
}
);
CometChat . getUnreadMessageCountForAllGroups (). then (
( unreadMessageCount : Object ) => {
console . log ( "Unread message count fetched" , unreadMessageCount );
},
( error : CometChat . CometChatException ) => {
console . log ( "Error in getting unread message count" , error );
}
);
It returns an object which will contain the GUID as the key and the unread message count as the value.
On Success — Returns an object with GUIDs as keys and unread counts as values:Unread Count Object: Parameter Type Description Sample Value {GUID}number Unread message count for each group 2 or 5
Example: {"group_1748415903905": 2, "group_1762515421478": 2, "group_1762515423434": 5} On Failure — Error Object: Parameter Type Description Sample Value codestring Error code "ERR_NOT_LOGGED_IN"namestring Error name "Not logged in"messagestring Error message "No user is currently logged in."detailsobject Additional error details {}
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
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