AI Integration Quick Reference
AI Integration Quick Reference
Available from Chat SDK v5.0.7. These APIs require
cometchat_sdk v5.0.7 or later. See Setup to upgrade.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:- Message-list fetches and
getMessageDetails()ask the server for the flag, and it arrives on the message as thethreadSubscribedfield. subscribeToThread()andunsubscribeFromThread()complete when the server has accepted the change. TheonSuccesscallback is the acknowledgement — there is no follow-up event.
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.
- Dart
Unsubscribe from a Thread
UseunsubscribeFromThread(). This is idempotent too — unsubscribing from a thread the user does not follow succeeds silently.
- Dart
Read the Subscription State
The state rides the parent message. Read it from thethreadSubscribed field:
- Dart
MessagesRequest fetches and getMessageDetails() send withThreadSubscribed=true, so any message you obtained from them carries the flag. Other responses do not ask for it — the message returned by pinMessage() or saveMessage(), and a conversation’s last message — so those read false. The flag is per-viewer: the same message yields different values for different users.
A message delivered over the socket, or returned by a call that did not ask for the flag, reads
false. That is not a claim that the user is unsubscribed — it means nobody asked. Because subscribing is idempotent, rendering that false as the “Subscribe” affordance is safe: an unnecessary subscribe is harmless.Re-read the State for One Message
When you need an authoritative flag for a single message — after acting on a socket-delivered message, or for a deep link to a thread you have not fetched — fetch the parent message withgetMessageDetails() and read the flag off the result.
- Dart
You Are Subscribed to Your Own Messages
Sending a message subscribes you to the thread it may later grow — there is nothing to call. A message you sent comes back withthreadSubscribed set to true 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 that asked for the flag. 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, align the copies you hold once you know the answer.threadSubscribed is a mutable field for exactly this:
- Dart
Reacting to Replies
A thread reply is an ordinary message withparentMessageId set, delivered through the standard MessageListener alongside every other message. There is no separate thread listener.
- Dart
MessageListener also gets you onMessageEdited and onMessageDeleted for replies, which a thread-only channel would not.
Replies sent from this device do not arrive on a listener — bump your thread row from the send call’s onSuccess callback instead. Replies the same user sends from another device do arrive on MessageListener.
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 — presence in the list is a subscription. ThreadsRequest, MessageThread and ConversationListener are exported from package:cometchat_sdk/cometchat_sdk.dart, not from the modular core.dart or messaging.dart entry points.
- Dart
fetchNext() takes required onSuccess and onError callbacks, like every other request in the SDK. Call it repeatedly on the same object to page through the list, using hasMore() as the loop condition — page on it rather than on the size of the last result. Once the list is exhausted, fetchNext() resolves an empty list.
- Dart
setUid() for a one-on-one counterpart or setGuid() for a group. The two are mutually exclusive.
- Dart
If you insert a row into the list yourself — for example after the user subscribes to a thread from the message view — call
threadsRequest.markSeen(parentMessageId) so the next fetchNext() does not return it again as a duplicate.
A
ThreadsRequest is single-use and one-directional: it accumulates its paging state internally and has no reset. To refresh a list, build a new request from the builder and replace the list rather than re-running an exhausted one. This is the same contract as ConversationsRequest and MessagesRequest.The MessageThread Model
Each row is aMessageThread:
Sort a thread inbox on
lastReply?.sentAt falling back to parentMessage?.sentAt — a thread with no replies has no last reply, and updatedAt is a cursor, not a sort key. A row carries only the raw receiverUid, with no name or avatar: render the ID immediately and resolve the display name lazily with CometChat.getUser() or CometChat.getGroup() rather than dropping the row.Notification Preferences
The notification preference for replies carries a value that pairs with this feature, so a user can be notified only about the threads they follow:SUBSCRIBE_TO_SUBSCRIBED_THREADS in the RepliesOptions enum.
Threads exist in one-on-one conversations as well as groups, so set it on whichever preferences you are updating:
- Dart
updatePreferences() merges what you set, so sending only the fields you changed is enough.QuotedRepliesOptions list.
Error Handling
The calls on this page report failures throughonError with a CometChatException. Branch on e.code rather than the message text:
- Dart
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
Notification Preferences
Read and update a user’s notification preferences