Skip to main content
// Filter by category and type
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID("UID")
  .setCategories(["message"])
  .setTypes(["image", "video", "audio", "file"])
  .setLimit(50)
  .build();

// Unread messages only
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID("UID")
  .setUnread(true)
  .setLimit(50)
  .build();

// Threaded messages
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID("UID")
  .setParentMessageId(parentId)
  .setLimit(50)
  .build();

// Fetch with pagination
messagesRequest.fetchPrevious().then(messages => { });
messagesRequest.fetchNext().then(messages => { });
Key methods: setUID(), setGUID(), setLimit(), setCategories(), setTypes(), setTags(), setUnread(), setParentMessageId(), setMessageId(), setTimestamp(), hideReplies(), hideDeletedMessages()
The MessagesRequest class fetches messages based on various parameters. It uses the Builder design pattern via MessagesRequestBuilder. To fetch messages:
  1. Create a MessagesRequestBuilder object
  2. Set your desired parameters
  3. Call build() to get a MessagesRequest object
  4. Call fetchNext() or fetchPrevious() to retrieve messages
MethodDescription
fetchNext()Returns messages after the specified parameters
fetchPrevious()Returns messages before the specified parameters
Messages are paginated with a maximum of 100 per request. Call fetchPrevious()/fetchNext() repeatedly on the same object to get subsequent pages.

Number of messages fetched

Set the number of messages to fetch per request using setLimit(). Maximum is 100.
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setLimit(50)
  .build();

Messages for a user conversation

Use setUID() to fetch messages between the logged-in user and a specific user.
let UID = "UID";
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(50)
  .build();
When messages are fetched successfully, the response includes an array of message objects.
The examples on this page use a small setLimit() value for brevity. In production, use a higher limit (up to 100) for efficient pagination.

Messages for a group conversation

Use setGUID() to fetch messages from a group. The logged-in user must be a member of the group.
let GUID = "GUID";
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setGUID(GUID)
  .setLimit(50)
  .build();
When messages are fetched successfully, the response includes an array of message objects.
If neither setUID() nor setGUID() is used, all messages for the logged-in user across all conversations will be fetched. All parameters below can be combined with setUID() or setGUID().

Messages before/after a message

Use setMessageId() to fetch messages before or after a specific message ID. Use fetchNext() to get messages after, or fetchPrevious() to get messages before.
let UID = "UID";
let messageId = 1;
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setMessageId(messageId)
  .setLimit(limit)
  .build();
This method can be combined with setUID() or setGUID() to fetch messages around a specific message in a conversation.

Messages before/after a given time

Use setTimestamp() with a Unix timestamp to fetch messages before or after a specific time.
let UID = "UID";
let timestamp = 1602221371;
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setTimestamp(timestamp)
  .setLimit(limit)
  .build();
This method can be combined with setUID() or setGUID() to fetch messages around a specific time in a conversation.

Unread messages

Use setUnread(true) to fetch only unread messages.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setUnread(true)
  .setLimit(limit)
  .build();
Combine with setGUID() or setUID() to fetch unread messages for a specific conversation.

Exclude messages from blocked users

Use hideMessagesFromBlockedUsers(true) to exclude messages from users you’ve blocked. Default is false.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .hideMessagesFromBlockedUsers(true)
  .setLimit(limit)
  .build();
This also works in group conversations where both users are members.

Updated and received messages

Use setUpdatedAfter() with a Unix timestamp to fetch messages that were sent or updated after a specific time. Updated messages include those marked as read/delivered, edited, or deleted.
let UID = "UID";
let limit = 30;
let timestamp = 1602221371;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setUpdatedAfter(timestamp)
  .setLimit(limit)
  .build();
Useful for syncing messages with a local database — fetch only what’s changed since your last sync.

Updated messages only

Use updatesOnly(true) with setUpdatedAfter() to fetch only updated messages (not newly received ones). This method must be used together with setUpdatedAfter().
let UID = "UID";
let limit = 30;
let timestamp = 1602221371;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setUpdatedAfter(timestamp)
  .updatesOnly(true)
  .setLimit(limit)
  .build();
When messages are fetched successfully, the response includes only messages that have been updated (edited, deleted, read/delivered status changed) after the specified timestamp.

Messages for multiple categories

Use setCategories() with an array of category names to filter by message category. See Message structure and hierarchy for available categories.
let UID = "UID";
let limit = 30;
let categories = ["message", "custom"];
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setCategories(categories)
  .setLimit(limit)
  .build();
The above snippet fetches only messages in the message and custom categories. Use this to exclude categories like call and action.

Messages for multiple types

Use setTypes() with an array of type names to filter by message type. See Message structure and hierarchy for available types.
let UID = "UID";
let limit = 30;
let categories = ["message"];
let types = ["image", "video", "audio", "file"];
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setCategories(categories)
  .setTypes(types)
  .setLimit(limit)
  .build();
The above snippet fetches all media messages (image, video, audio, file).

Messages for a specific thread

Use setParentMessageId() to fetch messages belonging to a specific thread.
let UID = "UID";
let messageId = 1;
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .setParentMessageId(messageId)
  .build();
The above code returns messages belonging to the thread with the specified parent message ID.

Hide threaded messages in user/group conversations

Use hideReplies(true) to exclude threaded messages from the main conversation. Default is false.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hideReplies(true)
  .build();

Hide deleted messages in user/group conversations

Use hideDeletedMessages(true) to exclude deleted messages. Default is false.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hideDeletedMessages(true)
  .build();

Hide quoted messages in user/group conversations

Use hideQuotedMessages(true) to exclude quoted messages. Default is false.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hideQuotedMessages(true)
  .build();

Messages by tags

Use setTags() with an array of tag names to fetch only messages with those tags.
let UID = "UID";
let limit = 30;
let tags = ["starredMessage"];
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .setTags(tags)
  .build();

Messages with tags

Use withTags(true) to include tag information in the response. Default is false.
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .withTags(true)
  .build();
When withTags(true) is set, each message’s tags field will be populated. Access tags using getTags().
Additional FieldGetterReturn TypeDescription
tagsgetTags()string[]Tags associated with the message
Use hasLinks(true) to fetch only messages containing links. Default is false.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hasLinks(true)
  .build();

Messages with attachments

Use hasAttachments(true) to fetch only messages with attachments (image, audio, video, or file). Default is false.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hasAttachments(true)
  .build();
The response contains media message objects with attachment details including file metadata and thumbnail URLs.

Messages with reactions

Use hasReactions(true) to fetch only messages that have reactions. Default is false.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hasReactions(true)
  .build();
The response contains message objects with reactions. Each message’s data object includes a reactions array.

Messages with mentions

Use hasMentions(true) to fetch only messages that contain mentions. Default is false.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .hasMentions(true)
  .build();
The response contains text message objects with mentions. Each message has a mentionedUsers array, a mentionedMe boolean, and a data.mentions object.

Messages with particular user mentions

Use setMentionedUIDs() with an array of UIDs to fetch messages that mention specific users.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .setMentionedUIDs(["UID"])
  .build();
The response contains text message objects that mention the specified users.

Messages with specific attachment types

Use setAttachmentTypes() with an array of CometChat.AttachmentType values to fetch messages with specific attachment types.
This feature is only available with Conversation & Advanced Search. 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)
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
  .setUID(UID)
  .setLimit(limit)
  .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO])
  .build();

The response contains media message objects filtered to the specified attachment types.

Next Steps

Send Messages

Send text, media, and custom messages

Receive Messages

Listen for incoming messages in real-time

Message Structure

Understand message categories, types, and hierarchy

Threaded Messages

Work with threaded conversations