Quick Reference - Send a threaded message and fetch thread replies:
Send Message in a Thread
As mentioned in the Send a Message section, you can either send a message to a user or a group based on thereceiverType and the UID/GUID specified for the message. A message can belong to any of the following types:
- Text Message
- Media Message
- Custom Message
parentMessageId must be set to indicate that the message belongs to the thread with the specified parentMessageId.
This can be achieved using the setParentMessageId() method provided by the object of the TextMessage, MediaMessage, and CustomMessage class. The id specified in the setParentMessageId() method maps the message sent to the particular thread.
Example to send a text message in a thread in a user conversation:
- JavaScript
- TypeScript
parentMessageId 100.
Similarly, using the setParentMessageId() method, Media and Custom Messages can be sent in threads too.
Receiving Real-Time Messages
The procedure to receive real-time messages is exactly the same as mentioned in the Receive Messages section. This can be achieved using theMessageListener class provided by the SDK.
To add a MessageListener, you can use the addMessageListener() method of the SDK. The only thing that needs to be checked is if the received message belongs to the active thread. This can be done using the parentMessageId field of the message object.
- JavaScript
- TypeScript
Fetch All Messages for a Thread
You can fetch all the messages belonging to a particular thread by using theMessagesRequest class. To get an object of the MessagesRequest class, use the MessagesRequestBuilder class and call the setParentMessageId() method to inform the SDK that you only need the messages belonging to the thread with the specified parentMessageId.
Once you have the object of the MessagesRequest class, call the fetchPrevious() method to get the latest messages in the thread. In one call, a maximum of 100 messages can be fetched. To fetch the next set of messages, call the fetchPrevious() method again on the same object.
- JavaScript
- TypeScript
Avoid Threaded Messages in User/Group Conversations
While fetching messages for normal user/group conversations using theMessagesRequest, threaded messages will be included in the list by default. To exclude threaded messages from the list of user/group messages, use the hideReplies() method of the MessagesRequestBuilder class. This method takes a boolean argument which, when set to true, excludes messages belonging to threads from the list of messages.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
cometchat-uid-1 excluding all the threaded messages belonging to the same conversation.
Best Practices and Troubleshooting
How do I identify which thread a message belongs to?
How do I identify which thread a message belongs to?
Every message object has a
parentMessageId field. If this field is set (non-zero), the message is part of a thread. Use message.getParentMessageId() to retrieve the parent message ID and match it against the active thread.Why am I receiving thread messages in my main conversation list?
Why am I receiving thread messages in my main conversation list?
By default,
MessagesRequest includes threaded messages. Use the hideReplies(true) method on MessagesRequestBuilder when fetching messages for the main conversation to exclude thread replies.What is the maximum number of messages I can fetch per request?
What is the maximum number of messages I can fetch per request?
You can fetch a maximum of 100 messages per
fetchPrevious() call. Use the setLimit() method on MessagesRequestBuilder to control the number of messages returned. To load more messages, call fetchPrevious() again on the same MessagesRequest object.Can I send media or custom messages in a thread?
Can I send media or custom messages in a thread?
Yes. The
setParentMessageId() method is available on TextMessage, MediaMessage, and CustomMessage objects. Set the parent message ID before calling CometChat.sendMessage() to send any message type within a thread.Next Steps
Send a Message
Learn how to send text, media, and custom messages to users and groups.
Receive Messages
Set up real-time message listeners and fetch message history.
Message Filtering
Filter messages by type, category, tags, timestamps, and more.
Message Structure
Understand message categories, types, and the message hierarchy.