AI Integration Quick Reference
AI Integration Quick Reference
Add a Reaction
CalladdReaction() with a message ID and an emoji string.
- TypeScript
- JavaScript
Remove a Reaction
CallremoveReaction() with the same message ID and emoji.
- TypeScript
- JavaScript
addReaction() and removeReaction() return a BaseMessage object with the updated reactions.
Read Reaction Data from a Message
AnyBaseMessage exposes reaction data through two methods:
Get All Reactions
UsegetReactions() to retrieve the list of reactions on a message. Returns an array of ReactionCount objects, or an empty array if no one has reacted.
- TypeScript
- JavaScript
Check if the Logged-in User Has Reacted
CallgetReactedByMe() on any ReactionCount object to check whether the logged-in user has reacted with that particular emoji. Returns true if they have, false otherwise.
- TypeScript
- JavaScript
Fetch Reactions for a Message
To get the full list of who reacted with what on a specific message, useReactionsRequestBuilder. You can paginate through results with fetchNext() and fetchPrevious() (default 10, maximum 1000 per request). A larger limit is sent as 1000.
Fetch Next
- TypeScript
- JavaScript
Fetch Previous
- TypeScript
- JavaScript
Offline Behaviour
With offline support enabled,ReactionsRequest.fetchNext() and fetchPrevious() first deliver onCachedReactions from the device when you pass a ReactionFetchListener (empty when nothing was recorded), then onServerReactions with the live page to reconcile by reaction id. Use a separate ReactionsRequest for each direction. Offline, a read ends with onError with code FAILED_TO_FETCH after the cached page.
The cached phase replays the page this request last received from the server online, in the same direction. It does not re-run these settings as a query against the cache.
Real-Time Reaction Events
Register aMessageListener to receive reaction events as they happen. This is how you keep your UI in sync when other users add or remove reactions.
Override onMessageReactionAdded and onMessageReactionRemoved. Each receives a ReactionEvent, whose getReaction() returns the Reaction and whose getReceiverId(), getReceiverType(), getConversationId() and getParentMessageId() identify where it happened.
- TypeScript
- JavaScript
Remove the Listener
Remove the listener with the same listener ID:- TypeScript
- JavaScript
Update Message With Reaction Info
When a user adds or removes a reaction, you will receive a real-time event. Once you receive it, you would want to update the message with the latest reaction information. To do so, useCometChat.CometChatHelper.updateMessageWithReactionInfo().
Pass the BaseMessage the reaction belongs to, the Reaction from the event (reactionEvent.getReaction()), and the action: CometChat.REACTION_ACTION.REACTION_ADDED from onMessageReactionAdded or CometChat.REACTION_ACTION.REACTION_REMOVED from onMessageReactionRemoved. It returns the message with its getReactions() updated.
- TypeScript
- JavaScript
BaseMessage object. After calling this method, use modifiedBaseMessage.getReactions() to get the latest reactions and refresh your UI.
Next Steps
Send Messages
Send text, media, and custom messages to users and groups
Receive Messages
Listen for incoming messages in real-time and fetch missed messages
Mentions
Mention specific users in messages
Threaded Messages
Organize conversations with message threads