Thread subscription builds on Threaded
Messages. A thread is identified by the ID
of its parent message — there is no separate thread ID.
How state works
The SDK keeps no subscription state of its own. There is no cache and no listener to reconcile:- Every message fetch asks the server for the flag, and it arrives on the message — read it with
message.isThreadSubscribed(). subscribeToThread()andunsubscribeFromThread()resolve when the server has accepted the change. The resolved promise is the acknowledgement.
Subscribe to a Thread
UsesubscribeToThread() with the ID of the thread’s parent message. The call is idempotent — subscribing to a thread the user already follows succeeds silently. Subscribing to a message with zero replies is allowed; the user is notified when the first reply arrives.
- TypeScript
- JavaScript
Unsubscribe from a Thread
UseunsubscribeFromThread(). This is idempotent too — unsubscribing from a thread the user does not follow succeeds silently.
- TypeScript
- JavaScript
Read the Subscription State
The state rides the parent message. Read it withisThreadSubscribed():
- TypeScript
- JavaScript
MessagesRequest or getMessageDetails() carries it.
A message delivered over the socket carries no flag and therefore reads
false. That is not a claim that the user is unsubscribed — it means nobody
asked. When you need certainty for a thread you have not fetched (a deep link,
for instance), fetch the parent message with CometChat.getMessageDetails() and
read the flag off the result.You are subscribed to your own messages
Sending a message subscribes you to the thread it may later grow — there is nothing to call. The message comes back withthreadSubscribed: true, both in the send response and on later fetches, and only for you: the flag is per-viewer, so the same message reads false for everybody else until they subscribe themselves.
That default is what makes the flag meaningful on your own messages. Since it starts out true, a false on a message you sent — read from a fetch, not the socket — is not silence. It means you unsubscribed, and nothing should quietly put you back.
This only holds for a message you sent and obtained from a fetch. On anyone else’s message, or on anything socket-delivered, false still just means the server was not asked.
Keeping your own copies in sync
The same thread can be represented by several message objects at once — a row in the message list, the header of an open thread view, an entry in a thread inbox. Because the SDK caches nothing, usesetThreadSubscribed() to align the copies you hold once you know the answer:
- TypeScript
- JavaScript
Reacting to Replies
A thread reply is an ordinary message withparentMessageId set, delivered through the standard MessageListener like any other message. There is no separate thread listener.
- TypeScript
- JavaScript
MessageListener also gets you onMessageEdited and onMessageDeleted for replies, which a thread-only channel would not.
Your own replies do not arrive on a listener — bump your thread row from the sendMessage() promise instead.
Fetch the Threads a User Participates In
UseThreadsRequest to build a thread inbox. Every returned thread is one the logged-in user is subscribed to.
- TypeScript
- JavaScript
fetchNext() repeatedly on the same object to page through the list, using hasMore() as the loop condition. Scope the list to one conversation with setUid() or setGuid() — the two are mutually exclusive.
- TypeScript
- JavaScript
hasMore() is what tells you the list has ended — page on it rather than on the size of the last result, which can be shorter than the limit without meaning you have reached the end.
The MessageThread Model
Each row is aMessageThread:
Sort a thread inbox on
getLastReply()?.getSentAt() falling back to the parent
message’s sentAt — a thread with no replies has no last reply.Error Handling
BothsubscribeToThread() and unsubscribeFromThread() reject with a CometChatException. The most common client-side failure is an invalid parent message ID.
- TypeScript
- JavaScript
Next Steps
Threaded Messages
Send, receive and fetch messages inside a thread
All Real Time Listeners
Every listener the SDK exposes, in one place
Mentions
Mention users in messages and filter for your own mentions
Additional Message Filtering
Filter messages by thread, type, tags and more