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
MessagesRequestBuilderclass. - Set all the parameters you wish to set.
- Call the
build()method of theMessagesRequestBuilderclass to get an object of theMessagesRequestclass.
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.
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:
Number of messages fetched
Set the number of messages to fetch per request usingsetLimit(). Maximum is 100.
- TypeScript
- JavaScript
Messages for a user conversation
UsesetUID() to fetch messages between the logged-in user and a specific user.
- TypeScript
- JavaScript
Messages for a group conversation
UsesetGUID() to fetch messages from a group. The logged-in user must be a member of the group.
- TypeScript
- JavaScript
Messages before/after a message
UsesetMessageId() to fetch messages before or after a specific message ID. Use fetchNext() to get messages after, or fetchPrevious() to get messages before.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
setUID() or setGUID() to fetch messages around a specific message in a conversation.
Messages before/after a given time
UsesetTimestamp() with a Unix timestamp to fetch messages before or after a specific time.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
setUID() or setGUID() to fetch messages around a specific time in a conversation.
Unread messages
UsesetUnread(true) to fetch only unread messages.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
setGUID() or setUID() to fetch unread messages for a specific conversation.
Exclude messages from blocked users
UsehideMessagesFromBlockedUsers(true) to exclude messages from users you’ve blocked. Default is false.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Updated and received messages
UsesetUpdatedAfter() 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.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Updated messages only
UseupdatesOnly(true) with setUpdatedAfter() to fetch only updated messages (not newly received ones). This method must be used together with setUpdatedAfter().
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages for multiple categories
UsesetCategories() with an array of category names to filter by message category. See Message structure and hierarchy for available categories.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
message and custom categories. Use this to exclude categories like call and action.
Messages for multiple types
UsesetTypes() with an array of type names to filter by message type. See Message structure and hierarchy for available types.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages for a specific thread
UsesetParentMessageId() to fetch messages belonging to a specific thread.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Hide threaded messages in user/group conversations
UsehideReplies(true) to exclude threaded messages from the main conversation. Default is false.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Hide deleted messages in user/group conversations
UsehideDeletedMessages(true) to exclude deleted messages. Default is false.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Hide quoted messages in user/group conversations
UsehideQuotedMessages(true) to exclude quoted messages. Default is false.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages by tags
UsesetTags() with an array of tag names to fetch only messages with those tags.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages with tags
UsewithTags(true) to include tag information in the response. Default is false.
- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
withTags(true) is set, each message’s tags field will be populated. Access tags using getTags().
| Additional Field | Getter | Return Type | Description |
|---|---|---|---|
| tags | getTags() | string[] | Tags associated with the message |
Messages with links
UsehasLinks(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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages with attachments
UsehasAttachments(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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages with reactions
UsehasReactions(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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
data object includes a reactions array.
Messages with mentions
UsehasMentions(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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
mentionedUsers array, a mentionedMe boolean, and a data.mentions object.
Messages with particular user mentions
UsesetMentionedUIDs() 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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
Messages with specific attachment types
UsesetAttachmentTypes() 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)- TypeScript (User)
- JavaScript (User)
- TypeScript (Group)
- JavaScript (Group)
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