Skip to main content
Messages that are started from a particular message are called Threaded messages or simply threads. Each Thread is attached to a message which is the Parent message for that thread.

Send Message in a Thread

As mentioned in the Send a Message section, you can send a message to a User or a Group by mentioning the receiver (uid/guid) and receiverType(user/group). A message can be categorized as:
  1. Text Message
  2. Media Message
  3. Custom Message.
Any of the above messages can be sent in a thread. As a thread is identified with a parent message, the parentMessageId must be set for the message. This will indicate that the message to be sent has to be a part of the thread of the message 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.
Starting with v5.1.0, the CallbackListener overloads of `sendMessage()` are deprecated. Use MessageSentListener instead; it requires SDK 5.1.0 or later. On 5.0.x, keep using CallbackListener and handle the sent message in onSuccess where these examples use onMessageSent. With offline support off, it behaves exactly like the old callback: onError is unchanged, and the sent message arrives in onMessageSent instead of onSuccess. With offline support on, onMessagePending fires first. A message sent without a connection stays PENDING and is sent when the connection returns: onError fires with code ERROR_INTERNET_UNAVAILABLE as a not-connected notice, and onMessageSent follows once the message is sent. In onError, check the message’s getStatus(): PENDING means queued, FAILED means it failed. The old CallbackListener overloads are never queued; offline, they end with onError. See Send While Offline.
The above snippet shows how a message with the text “Hello” can be sent in the thread with 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. This can be achieved using the MessageListener 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 parentMessageId field of the message object.

Fetch all the messages for any particular thread.

You can fetch all the messages belonging to a particular thread by using the MessagesRequest class. The MessageRequestBuilder builds the MessageRequest object using the following functions:
  1. setParentMessageId(): Takes the parentId of the message as argument whose thread needs to be requested.
  2. build(): returns the MessageRequest object.
Once you have the MessagesRequest object, you can call the fetchPrevious() method to get the latest messages in the thread. In one iteration, a maximum of 100 messages can be fetched. If you wish to fetch the next set of messages, you need to call the fetchPrevious() method again on the same object.
Starting with v5.1.0, the CallbackListener overload of `fetchPrevious()` / `fetchNext()` is deprecated. Use `MessageFetchListener` instead; it requires SDK 5.1.0 or later, so on 5.0.x keep using CallbackListener. With offline support off, it behaves exactly like the old callback: onError is unchanged, and the server result arrives in the onServer… callback instead of onSuccess. With offline support on, it also delivers the cached result first; the old overload keeps working but receives only the server result. See Show Cached Data.

Avoid Threaded Messages in User/Group Conversations

While fetching messages for normal user/group conversations using the MessagesRequest, the threaded messages by default will be a part of the list of messages received. In order to exclude the threaded messages from the list of user/group messages, you need to use the hideReplies() method of the MessagesRequestBuilder class. This method takes a boolean argument which when set to true excludes the messages belonging to threads from the list of messages.
The above snippet will return messages between the logged in user and cometchat-uid-1 excluding all the threaded messages belonging to the same conversation.

Offline Behaviour

With offline support enabled, thread fetches run in two phases: onCachedMessages delivers the cached thread page first (even when empty), then onServerMessages delivers the reconciled server copy. Offline, the fetch ends with onError with code ERROR_INTERNET_UNAVAILABLE after the cached page. A thread read is answered from the cache even when both setUID and setGUID are set; unlike a non-thread read, that pairing is not server-only here. See Show Cached Data.

Subscribe to a Thread

Users can subscribe to or unsubscribe from a thread to control whether they are notified about its replies, and you can fetch the list of threads a user participates in to build a thread inbox. See Thread Subscription.