Learn how to use MessagesRequestBuilder to filter and fetch messages by conversation, type, category, tags, timestamps, and more in the CometChat React Native SDK.
Quick Reference for AI Agents & Developers
Report incorrect code
Copy
Ask AI
// Fetch messages for a user conversationlet request = new CometChat.MessagesRequestBuilder().setUID("UID").setLimit(50).build();// Fetch messages for a group conversationlet request = new CometChat.MessagesRequestBuilder().setGUID("GUID").setLimit(50).build();// Fetch only unread messageslet request = new CometChat.MessagesRequestBuilder().setUID("UID").setUnread(true).setLimit(30).build();// Fetch only media messageslet request = new CometChat.MessagesRequestBuilder().setUID("UID").setCategories(["message"]).setTypes(["image", "video", "audio", "file"]).setLimit(30).build();// Fetch messages, then paginatelet messages = await request.fetchPrevious();
The MessagesRequest class as you must be familiar with helps you to fetch messages based on the various parameters provided to it. This document will help you understand better the various options that are available using the MessagesRequest class.
The MessagesRequest class is designed using the Builder design pattern. In order to obtain an object of the MessagesRequest class, you will have to make use of the MessagesRequestBuilder class in the MessagesRequest class.The MessagesRequestBuilder class allows you to set various parameters to the MessagesRequest class based on which the messages are fetched.Steps to generate an object of the MessagesRequest class:
Create an object of the MessagesRequestBuilder class.
Set all the parameters you wish to set.
Call the build() method of the MessagesRequestBuilder class to get an object of the MessagesRequest class.
Once you have an object of the MessagesRequest class, you can call either the fetchNext() method or the fetchPrevious() method using the object.
fetchNext() - Calling this method will return the messages after the specified parameters.
fetchPrevious() - Calling this method will give you messages before the specified parameters.
Since messages are obtained in a paginated manner, a maximum of 100 messages can be pulled in a single iteration. Calling the fetchPrevious()/fetchNext() method on the same MessagesRequest object will get you the next set of messages.Now that you are clear how to use the MessagesRequest class, below are the various options available:
In other words, how do I set the number of messages fetched in a single iterationTo achieve this, you can use the setLimit() method. This method takes an integer value as the input and informs the SDK to fetch the specified number of messages in one iteration. The maximum number of messages that can be fetched in one go is 100.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
let messagesRequest = new CometChat.MessagesRequestBuilder() .setLimit(50) .build();
Report incorrect code
Copy
Ask AI
let messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder().setLimit(50).build();
Response
On Success — fetchPrevious() returns an array of Message objects:Message Object (per item in array):
In other words, how do I fetch messages between me and any userThis can be achieved using the setUID() method. This method takes the UID of the user with whom the conversation is to be fetched. When a valid UID is passed, the SDK will return all the messages that are a part of the conversation between the logged-in user and the UID that has been specified.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
let UID = "UID";let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(50) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder().setUID(UID).setLimit(50).build();
Response
On Success — fetchPrevious() returns an array of Message objects:Message Object (per item in array):
In other words, how do I fetch messages for any group conversationYou can achieve this using the setGUID() method. This method takes the GUID of a group for which the conversations are to be fetched. Passing a valid GUID to this method will return all the messages that are a part of the group conversation. Please note that the logged-in user must be a member of the group to fetch the messages for that group.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(50) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder().setGUID(GUID).setLimit(50).build();
Response
On Success — fetchPrevious() returns an array of Message objects for the group:Message Object (per item in array):
If none of the above two methods setUID() and setGUID() is used, all the messages for the logged-in user will be fetched. This means that messages from all the one-on-one and group conversations which the logged-in user is a part of will be returned.> All the parameters discussed below can be used along with the setUID() or setGUID() or without any of the two to fetch all the messages that the logged-in user is a part of.
In other words, how do I fetch messages before or after a particular messageThis can be achieved using the setMessageId() method. This method takes the message-id as input and provides messages only after/before the message-id based on if the fetchNext() or fetchPrevious() method is triggered.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let messageId = 1;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setMessageId(messageId) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let messageId = 1;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setMessageId(messageId) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", messageId: number = 1, limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setMessageId(messageId) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", messageId: number = 1, limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setMessageId(messageId) .setLimit(limit) .build();
Response
On Success — fetchPrevious() returns messages before the specified message ID:Message Object (per item in array):
This method can be used along with setUID() or setGUID() methods to fetch messages after/before any specific message-id for a particular user/group conversation.
In other words, how do I fetch messages before or after a particular date or timeYou can easily achieve this using the setTimestamp() method. This method takes the Unix timestamp as input and provides messages only after/before the timestamp based on if fetchNext() or fetchPrevious() method is triggered.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let timestamp = 1602221371;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setTimestamp(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let timestamp = 1602221371;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setTimestamp(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let timestamp = 1602221371;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setTimestamp(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let timestamp = 1602221371;let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setTimestamp(timestamp) .setLimit(limit) .build();
Response
On Success — fetchPrevious() returns messages before the specified timestamp (includes custom message types):Message Object (per item in array):
This method can be used along with setUID() or setGUID() methods to fetch messages after/before any specific date or time for a particular user/group conversation.
In other words, how do I fetch unread messagesThis can easily be achieved using setting the unread flag to true. For this, you need to use the setUnread() method. This method takes a boolean value as input. If the value is set to true, the SDK will return just the unread messages.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUnread(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUnread(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID";let limit: number = 30;let messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUnread(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUnread(true) .setLimit(limit) .build();
Response
On Success — fetchPrevious() returns only unread messages:Message Object (per item in array):
In other words, how do I fetch messages excluding the messages from the users I have blockedThis can be easily achieved using the hideMessagesFromBlockedUsers() method. This method accepts a boolean value which determines if the messages from users blocked by the logged-in user need to be a part if the fetched messages. If the value is set to true, the messages will be hidden and won’t be a part of the messages fetched. The default value is false i.e if this method is not used, the messages from blocked users will be included in the fetched messages.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .hideMessagesFromBlockedUsers(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .hideMessagesFromBlockedUsers(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .hideMessagesFromBlockedUsers(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .hideMessagesFromBlockedUsers(true) .setLimit(limit) .build();
Response
On Success — fetchPrevious() returns messages excluding those from blocked users (includes group-call custom messages):Message Object (per item in array):
In other words, how do I fetch messages that have been received or updated after a particular date or timeThis method accepts a Unix timestamp value and will return all the messages that have been updated and the ones that have been sent/received after the specified time. The messages updated could mean the messages that have been marked as read/delivered or if the messages are edited or deleted.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let timestamp = 1602221371;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUpdatedAfter(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let timestamp = 1602221371;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUpdatedAfter(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let timestamp = 1602221371;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUpdatedAfter(timestamp) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, timestamp: string = "1602221371", messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUpdatedAfter(timestamp) .setLimit(limit) .build();
Response
On Success — fetchNext() returns messages received or updated after the specified timestamp:Message Object (per item in array):
This can be useful in finding the messages that have been received or updated after a certain time. Can prove very useful if you are saving the messages locally and would like to know the messages that have been updated or received after the last message available in your local databases.
In other words, how do I fetch messages that have been updated after a particular date or timeThis can be achieved easily by setting the updatesOnly parameter to true. To do so, you can use the updatesOnly() method. This method takes a boolean input and can be used with the setUpdatedAfter() method to get just the updated messages and not the messages sent/received after the specified time. This method cannot be used independently and always needs to be used with the setUpdatedAfter() method.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let timestamp = 1602221371;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUpdatedAfter(timestamp) .updatesOnly(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let timestamp = 1602221371;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUpdatedAfter(timestamp) .updatesOnly(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, timestamp: string = "1602221371", messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setUpdatedAfter(timestamp) .updatesOnly(true) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, timestamp: string = "1602221371", messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setUpdatedAfter(timestamp) .updatesOnly(true) .setLimit(limit) .build();
Response
On Success — fetchNext() returns only updated messages (e.g., read receipt changes, edits, deletions):Message Object (per item in array):
In other words, how do I fetch messages belonging to multiple categoriesWe recommend before trying this, you refer to the Message structure and hierarchy guide to get familiar with the various categories of messages.For this, you will have to use the setCategories() method. This method accepts a list of categories. This tells the SDK to fetch messages only belonging to these categories.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let categories = ["message", "custom"];let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setCategories(categories) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let categories = ["message", "custom"];let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setCategories(categories) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, categories: Array<String> = ["message", "custom"], messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setCategories(categories) .setLimit(limit) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, categories: Array<String> = ["message", "custom"], messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setCategories(categories) .setLimit(limit) .build();
Response
On Success — fetchPrevious() returns messages from the specified categories (includes both custom and message categories):Message Object (per item in array):
The above snippet will help you get only the messages belonging to the message and custom category. This can also be used to disable certain categories of messages like call and action. This along with setGUID() and setUID() can help display only the messages you wish to display avoiding the other category of messages.
In other words, how do I fetch messages belonging to multiple typesWe recommend before trying this, you refer to the Message structure and hierarchy guide to get familiar with the various types of messages.This can be easily achieved using the setTypes() method. This method accepts a list of types. This tells the SDK to fetch messages only belonging to these types.
Using the above code snippet, you can fetch all the media messages. This along with setUID() or setGUID() can be used to fetch media messages for any particular conversation. This can be useful in many other scenarios as well.
In other words, how do I fetch messages that are a part of a thread and not directly a user/group conversationThis can be done using the setParentMessageId() method. This method needs to be used when you have implemented threaded conversations in your app. This method will return the messages only belonging to the thread with the specified parent Id.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let messageId = 1; // Use msg.getId() on a message where msg.getReplyCount() > 0let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setParentMessageId(messageId) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let messageId = 1; // Use msg.getId() on a message where msg.getReplyCount() > 0let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setParentMessageId(messageId) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messageId: number = 1, // Use msg.getId() on a message where msg.getReplyCount() > 0 messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setParentMessageId(messageId) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messageId: number = 1, // Use msg.getId() on a message where msg.getReplyCount() > 0 messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setParentMessageId(messageId) .build();
Response
On Success — fetchPrevious() returns messages belonging to the specified thread:Message Object (per item in array):
Hide threaded messages in user/group conversations
In other words, how do I exclude threaded messages from the normal user/group conversationsIn order to do this, you can use the hideReplies() method. This method is also related to threaded conversations. This method takes boolean as input. This boolean when set to true will make sure that the messages that belong to threads are not fetched. If set to false, which is also the default value, the messages belong to the threads will also be fetched along with other messages.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hideReplies(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hideReplies(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hideReplies(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hideReplies(true) .build();
Response
On Success — fetchPrevious() returns messages excluding threaded replies (parent messages with threads still appear):Message Object (per item in array):
In other words, how do I exclude deleted messages from a user/group conversationIn order to do this, you can use the hideDeletedMessages() method. This method takes boolean as input. This boolean when set to true will make sure that the deleted messages are not fetched. If set to false, which is also the default value, the deleted messages will also be fetched along with other messages.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hideDeletedMessages(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hideDeletedMessages(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hideDeletedMessages(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hideDeletedMessages(true) .build();
Response
On Success — fetchPrevious() returns messages excluding deleted messages:Message Object (per item in array):
In other words, how do I fetch messages by tagsIn order to do this, you can use the setTags() method. This method accepts a list of tags. This tells the SDK to fetch messages only belonging to these tags.
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let tags = ["starredMessage"];let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setTags(tags) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let tags = ["starredMessage"];let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setTags(tags) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, tags: Array<String> = ["starredMessage"], messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setTags(tags) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, tags: Array<String> = ["starredMessage"], messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setTags(tags) .build();
Response
On Success — fetchPrevious() returns only messages matching the specified tags:Message Object (per item in array):
In other words, how do I fetch messages with the tags informationIn order to do this, you can use the withTags() method. This method accepts boolean as input. When set to true , the SDK will fetch messages along with the tags. When set to false , the SDK will not fetch tags associated with messages. The default value for this parameter is false .
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .withTags(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .withTags(tags) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .withTags(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .withTags(true) .build();
Response
On Success — fetchPrevious() returns messages with the tags field included:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that contain links?In order to do this, you can use the hasLinks() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have links in the text. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hasLinks(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasLinks(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .hasLinks(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasLinks(true) .build();
Response
On Success — fetchPrevious() returns an array of Message objects containing links:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that contain attachments?In order to do this, you can use the hasAttachments() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have attachments (image, audio, video or file). The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hasAttachments(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasAttachments(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .hasAttachments(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasAttachments(true) .build();
Response
On Success — fetchPrevious() returns an array of Message objects with attachments:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that contain reactions?In order to do this, you can use the hasReactions() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have reactions. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hasReactions(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasReactions(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .hasReactions(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasReactions(true) .build();
Response
On Success — fetchPrevious() returns an array of Message objects with reactions:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that contain mentions?In order to do this, you can use the hasMentions() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have mentions. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .hasMentions(true) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasMentions(true) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .hasMentions(true) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .hasMentions(true) .build();
Response
On Success — fetchPrevious() returns an array of Message objects containing mentions:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that mention specific users?In order to do this, you can use the setMentionedUIDs() method. This method accepts an array of UIDs as input. When set, the SDK will fetch messages which have the mentions of the UIDs passed.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setMentionedUIDs(["UID"]) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setMentionedUIDs(["UID"]) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .setMentionedUIDs(["UID"]) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setMentionedUIDs(["UID"]) .build();
Response
On Success — fetchPrevious() returns an array of Message objects mentioning the specified UIDs:Message Object (per item in array):
In other words, as a logged-in user, how do I fetch messages that contain specific types of attachments?In order to do this, you can use the setAttachmentTypes() method. This method accepts an array of CometChat.AttachmentType ENUM values as input. When provided, the SDK will fetch only those messages that include attachments of the specified types (such as image, audio, video, or file).
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
To User
To Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let UID = "UID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO]) .build();
Report incorrect code
Copy
Ask AI
let GUID = "GUID";let limit = 30;let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO]) .build();
Report incorrect code
Copy
Ask AI
let UID: string = "UID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(UID) .setLimit(limit) .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO]) .build();
Report incorrect code
Copy
Ask AI
let GUID: string = "GUID", limit: number = 30, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO]) .build();
Response
On Success — fetchPrevious() returns an array of Message objects with the specified attachment types:Message Object (per item in array):
Always call fetchNext() or fetchPrevious() on the same MessagesRequest object to paginate through results. Creating a new MessagesRequest object will reset pagination and start from the beginning.
Combine filters for precise results
You can chain multiple builder methods together (e.g., setCategories() + setTypes() + setUID()) to narrow down results. This is more efficient than fetching all messages and filtering client-side.
Use setUpdatedAfter() for local caching
If you store messages locally, use setUpdatedAfter() with the timestamp of your last synced message to fetch only new or updated messages. Combine with updatesOnly(true) if you only need edits, deletions, and read/delivery status changes.
Set an appropriate limit
The maximum limit per fetch is 100. For most UI use cases, a limit of 30–50 provides a good balance between performance and user experience. Smaller limits mean faster responses and less memory usage.
Troubleshooting: No messages returned
If fetchNext() or fetchPrevious() returns an empty array, verify that: the logged-in user is a member of the group (for group conversations), the UID/GUID is correct, and the applied filters aren’t too restrictive. Try removing filters one at a time to isolate the issue.
Troubleshooting: Conversation & Advanced Search features not working
Methods like hasLinks(), hasAttachments(), hasReactions(), hasMentions(), setMentionedUIDs(), and setAttachmentTypes() require the Conversation & Advanced Search feature to be enabled. Ensure you are on an Advanced or Custom plan and have enabled the feature from the CometChat Dashboard (Chats → Settings → General Configuration).