Skip to main content
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");
Available via: SDK | REST API | UI Kits
Receiving messages with CometChat has two parts:
  1. Adding a listener to receive real-time messages when your app is running
  2. 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);
    },
  })
);
ParameterDescription
listenerIdAn ID that uniquely identifies that listener.
onTextMessageReceived — Text message object received via the listener:TextMessage Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25195"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Hello"
sentAtnumberTimestamp when sent1771386431
updatedAtnumberTimestamp when updated1771386431
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386392
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386383
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Hello"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓
moderationobjectModeration statusSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386392
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386383
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

data.metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

data.metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

data.metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

data.moderation Object:
ParameterTypeDescriptionSample Value
statusstringModeration status"approved"

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

onMediaMessageReceived — Media message object received via the listener:MediaMessage Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25196"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMedia type"image"
categorystringMessage category"message"
sentAtnumberTimestamp when sent1771386517
updatedAtnumberTimestamp when updated1771386517
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771386507
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771386436
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
typestringMedia type"image"
categorystringMessage category"message"
urlstringMedia file URL"https://data-in.cometchat.io/..."
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
attachmentsarrayMedia attachmentsSee below ↓
entitiesobjectSender and receiver entitiesSee below ↓
moderationobjectModeration statusSee below ↓

data.attachments Array (per item):
ParameterTypeDescriptionSample Value
urlstringFile URL"https://data-in.cometchat.io/..."
namestringFile name"photo.jpg"
mimeTypestringMIME type"image/jpeg"
extensionstringFile extension"jpg"
sizenumberFile size in bytes142099

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771386507
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771386436
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.moderation Object:
ParameterTypeDescriptionSample Value
statusstringModeration status"approved"

onCustomMessageReceived — Custom message object received via the listener:CustomMessage Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25197"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringCustom message type"test-custom"
categorystringMessage category"custom"
customDataobjectCustom data payloadSee below ↓
sentAtnumberTimestamp when sent1771386648
updatedAtnumberTimestamp when updated1771386648
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386640
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386630
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

customData Object:
ParameterTypeDescriptionSample Value
greetingstringCustom greeting text"Hello from custom message!"
timestampnumberCustom timestamp1771386646780

data Object:
ParameterTypeDescriptionSample Value
textstringDisplay text"Sent a custom message"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
customDataobjectCustom data payloadSee below ↓
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓
moderationobjectModeration statusSee below ↓

data.customData Object:
ParameterTypeDescriptionSample Value
greetingstringCustom greeting text"Hello from custom message!"
timestampnumberCustom timestamp1771386646780

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386640
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771386630
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

data.metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

data.metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

data.metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

data.moderation Object:
ParameterTypeDescriptionSample Value
statusstringModeration status"approved"

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted 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);
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);
  }
);
On Success — Returns an array of missed message objects:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25211"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Missed Message"
sentAtnumberTimestamp when sent1771388912
updatedAtnumberTimestamp when updated1771388912
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771388888
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Missed Message"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771388888
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

data.metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

data.metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extension{"links": []}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
On Success — Returns an array of missed group message objects:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25215"
conversationIdstringConversation identifier"group_group_1748415903905"
receiverIdstringGroup GUID"group_1748415903905"
receiverTypestringType of receiver"group"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Group Missed"
sentAtnumberTimestamp when sent1771389682
updatedAtnumberTimestamp when updated1771389682
senderobjectSender user detailsSee below ↓
receiverobjectReceiver group detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object (Group):
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
isBannedbooleanWhether current user is bannedfalse
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Group Missed"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"group"
entityobjectGroup entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

data.metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions data{"link-preview": {"links": []}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
On Success — Returns an array of unread message objects:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25216"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Unread Message"
sentAtnumberTimestamp when sent1771390060
deliveredAtnumberTimestamp when delivered1771390061
updatedAtnumberTimestamp when updated1771390061
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771389781
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Unread Message"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771389781
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

data.metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions data{"link-preview": {"links": []}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
On Success — Returns an array of unread group message objects:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25217"
conversationIdstringConversation identifier"group_group_1748415903905"
receiverIdstringGroup GUID"group_1748415903905"
receiverTypestringType of receiver"group"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Unread Group Message"
sentAtnumberTimestamp when sent1771390129
updatedAtnumberTimestamp when updated1771390129
senderobjectSender user detailsSee below ↓
receiverobjectReceiver group detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object (Group):
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
isBannedbooleanWhether current user is bannedfalse
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Unread Group Message"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"group"
entityobjectGroup entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions data{"extensions": {"link-preview": {"links": []}}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
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):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25220"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Historic 1-1"
sentAtnumberTimestamp when sent1771390691
updatedAtnumberTimestamp when updated1771390691
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771389781
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Historic 1-1"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"offline"
lastActiveAtnumberLast active timestamp1771389781
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
tagsarrayUser tags[]

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions data{"extensions": {"link-preview": {"links": []}}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
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):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25218"
conversationIdstringConversation identifier"group_group_1748415903905"
receiverIdstringGroup GUID"group_1748415903905"
receiverTypestringType of receiver"group"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"Historic Message 1"
sentAtnumberTimestamp when sent1771390631
updatedAtnumberTimestamp when updated1771390631
senderobjectSender user detailsSee below ↓
receiverobjectReceiver group detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object (Group):
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
isBannedbooleanWhether current user is bannedfalse
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"Historic Message 1"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadataSee below ↓

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771388895
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"group"
entityobjectGroup entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count1
hasJoinedbooleanWhether current user has joinedtrue
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data.metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions data{"extensions": {"link-preview": {"links": []}}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions dataSee below ↓

metadata.@injected Object:
ParameterTypeDescriptionSample Value
extensionsobjectExtensions dataSee below ↓

metadata.@injected.extensions Object:
ParameterTypeDescriptionSample Value
link-previewobjectLink preview extensionSee below ↓

metadata.@injected.extensions.link-preview Object:
ParameterTypeDescriptionSample Value
linksarrayExtracted links[]

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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).
FeatureBasic SearchAdvanced 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);
  }
);
On Success — Returns an array of messages matching the search keyword:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25224"
conversationIdstringConversation identifier"cometchat-uid-2_user_cometchat-uid-3"
receiverIdstringReceiver’s UID"cometchat-uid-2"
receiverTypestringType of receiver"user"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"xq7SearchTest"
sentAtnumberTimestamp when sent1771392432
deliveredAtnumberTimestamp when delivered1771392432
readAtnumberTimestamp when read1771392432
updatedAtnumberTimestamp when updated1771392432
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771391162
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-2"
namestringUser’s display name"George Alan"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771391157
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"xq7SearchTest"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesSee below ↓
metadataobjectInjected metadata{"@injected": {"extensions": {"link-preview": {"links": []}}}}

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperContains entityType and entity
receiverobjectReceiver entity wrapperContains entityType and entity

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions data{"extensions": {"link-preview": {"links": []}}}

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
  }
);
On Success — Returns an array of group messages matching the search keyword:Message Object (per item in array):
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25225"
conversationIdstringConversation identifier"group_group_1748415903905"
receiverIdstringGroup GUID"group_1748415903905"
receiverTypestringType of receiver"group"
typestringMessage type"text"
categorystringMessage category"message"
textstringMessage text content"xq8SearchTest"
sentAtnumberTimestamp when sent1771392513
updatedAtnumberTimestamp when updated1771392513
senderobjectSender user detailsSee below ↓
receiverobjectReceiver group detailsSee below ↓
dataobjectAdditional message dataSee below ↓
metadataobjectMessage metadataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-3"
namestringUser’s display name"Nancy Grace"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"
rolestringUser’s role"default"
statusstringUser’s online status"online"
lastActiveAtnumberLast active timestamp1771391162
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object (Group):
ParameterTypeDescriptionSample Value
guidstringGroup’s unique identifier"group_1748415903905"
namestringGroup’s display name"3 People Group"
typestringGroup type"public"
conversationIdstringConversation identifier"group_group_1748415903905"
ownerstringGroup owner UID"123456"
scopestringCurrent user’s scope in group"admin"
membersCountnumberTotal members in group12
onlineMembersCountnumberOnline members count2
hasJoinedbooleanWhether current user has joinedtrue
isBannedbooleanWhether current user is bannedfalse
joinedAtnumberTimestamp when user joined1749203133
createdAtnumberGroup creation timestamp1748415957
updatedAtnumberGroup update timestamp1771245340

data Object:
ParameterTypeDescriptionSample Value
textstringMessage text"xq8SearchTest"
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
entitiesobjectSender and receiver entitiesContains sender and receiver wrappers
metadataobjectInjected metadata{"@injected": {"extensions": {"link-preview": {"links": []}}}}

metadata Object:
ParameterTypeDescriptionSample Value
@injectedobjectInjected extensions data{"extensions": {"link-preview": {"links": []}}}

On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
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 = "UID";

CometChat.getUnreadMessageCountForUser(UID).then(
  (unreadMessageCountObject) => {
    console.log("Unread message count fetched", unreadMessageCountObject);
  },
  (error) => {
    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:
ParameterTypeDescriptionSample Value
{UID}numberUnread message count for the specified user2
Example: {"cometchat-uid-3": 2}
On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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);
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 = "GUID";

CometChat.getUnreadMessageCountForGroup(GUID).then(
  (unreadMessageCountObject) => {
    console.log("Unread message count fetched", unreadMessageCountObject);
  },
  (error) => {
    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:
ParameterTypeDescriptionSample Value
{GUID}numberUnread message count for the specified group3
Example: {"group_1748415903905": 3}
On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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();
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);
CometChat.getUnreadMessageCount().then(
  (unreadMessageCountObject) => {
    console.log("Unread message count fetched", unreadMessageCountObject);
  },
  (error) => {
    console.log("Error in getting unread message count", error);
  }
);
It returns an object having two keys:
  1. users - The value for this key holds another object that holds the UID as key and their corresponding unread message count as value.
  2. 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:
ParameterTypeDescriptionSample Value
usersobjectObject with UIDs as keys and unread counts as valuesSee below ↓
groupsobjectObject with GUIDs as keys and unread counts as valuesSee below ↓

users Object:
ParameterTypeDescriptionSample Value
{UID}numberUnread count for each user2
Example: {"cometchat-uid-3": 2}
groups Object:
ParameterTypeDescriptionSample Value
{GUID}numberUnread count for each group2
Example: {"group_1748415903905": 2}
On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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();
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);
CometChat.getUnreadMessageCountForAllUsers().then(
  (unreadMessageCountObject) => {
    console.log("Unread message count fetched", unreadMessageCountObject);
  },
  (error) => {
    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:
ParameterTypeDescriptionSample Value
{UID}numberUnread message count for each user2 or 3
Example: {"cometchat-uid-3": 2, "cometchat-uid-4": 3}
On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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();
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);
CometChat.getUnreadMessageCountForAllGroups().then(
  (unreadMessageCountObject) => {
    console.log("Unread message count fetched", unreadMessageCountObject);
  },
  (error) => {
    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:
ParameterTypeDescriptionSample Value
{GUID}numberUnread message count for each group2 or 5
Example: {"group_1748415903905": 2, "group_1762515421478": 2, "group_1762515423434": 5}
On Failure — Error Object:
ParameterTypeDescriptionSample Value
codestringError code"ERR_NOT_LOGGED_IN"
namestringError name"Not logged in"
messagestringError message"No user is currently logged in."
detailsobjectAdditional 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