Quick Reference for AI Agents & Developers
Add reaction: CometChat.addReaction(messageId:reaction:onSuccess:onError:)
Remove reaction: CometChat.removeReaction(messageId:reaction:onSuccess:onError:)
Fetch reactions: ReactionsRequestBuilder().setMessageId(messageId:).build() → fetchNext(onSuccess:onError:)
Listen for reactions: onMessageReactionAdded(_:), onMessageReactionRemoved(_:) in message listener
Related: Send Message · Messaging Overview
Enhance user engagement in your chat application with message reactions. Users can express their emotions using reactions to messages. This feature allows users to add or remove reactions, and to fetch all reactions on a message.
Add a Reaction
Users can add a reaction to a message by calling addReaction with the message ID and the reaction emoji.
CometChat. addReaction ( messageId : 148 , reaction : "😴" ) { message in
print ( "Reactions: \( message. getReactions () ) " )
} onError : { error in
print ( "Error: \( error ? . errorDescription ) " )
}
Sample Payloads - Add Reaction
Request
Success Response
Error Response
Method: CometChat.addReaction(messageId:reaction:)Parameter Type Value messageId Int38230reaction String"👍"
Message Properties: Parameter Type Value id Int38230reactionAdded String"👍"
Reactions List (message.reactions): Reaction Count ReactedByMe 👍 1true
Object Type: CometChatExceptionParameter Type Value errorCode String"ERR_MESSAGE_NOT_FOUND"errorDescription String"Message does not exist"
More Sample Payloads - Multiple Reactions
Add Heart Reaction
Add Laugh Reaction
Request: Parameter Type Value messageId Int38230reaction String"❤️"
Updated Reactions List: Reaction Count ReactedByMe 👍 1true❤️ 1true
Request: Parameter Type Value messageId Int38230reaction String"😂"
Updated Reactions List: Reaction Count ReactedByMe 👍 1true❤️ 1true😂 1true
You can react on Text, Media and Custom messages.
Remove a Reaction
Removing a reaction from a message can be done using the removeReaction method.
CometChat. removeReaction ( messageId : 148 , reaction : "😴" ) { message in
print ( "Reactions: \( message. getReactions () ) " )
} onError : { error in
print ( "Error: \( error ? . errorDescription ) " )
}
Sample Payloads - Remove Reaction
Request
Success Response
Error Response
Method: CometChat.removeReaction(messageId:reaction:)Parameter Type Value messageId Int38230reaction String"👍"
Message Properties: Parameter Type Value id Int38230reactionRemoved String"👍"
Updated Reactions List: Reaction Count ReactedByMe ❤️ 1true😂 1true
Object Type: CometChatExceptionParameter Type Value errorCode String"ERR_REACTION_NOT_FOUND"errorDescription String"Reaction not found on message"
Fetch Reactions for a Message
To get all reactions for a specific message, create a ReactionsRequest using ReactionsRequestBuilder.
Method Description setMessageId(messageId: Int)Specifies the message ID (required) setReaction(reaction: String)Filter by specific emoji (optional) setLimit(limit: Int)Number of reactions to fetch (max 100)
Fetch Next Reactions
let reactionsRequest = ReactionsRequestBuilder ()
. setLimit ( limit : 30 )
. setMessageId ( messageId : 148 )
. build ()
reactionsRequest. fetchNext { reactions in
for reaction in reactions {
print ( "Reaction: \( reaction. reaction ) " )
print ( "Reacted by: \( reaction. reactedBy ? . name ?? "" ) " )
}
} onError : { error in
print ( "Error: \( error ? . errorDescription ) " )
}
Sample Payloads - Fetch Reactions
Request
Success Response
Error Response
Method: ReactionsRequest.fetchNext()Parameter Type Value messageId Int38230limit Int30reaction String?nil (optional - filter by emoji)
Reactions Array: Reaction Reacted By UID Reacted By Name Reacted At 😴 "cometchat-uid-1""John Doe"1697025960👍 "cometchat-uid-2""Jane Smith"1697025950
Object Type: CometChatExceptionParameter Type Value errorCode String"ERROR_LIMIT_EXCEEDED"errorDescription String"Limit Exceeded Max limit of 100"
Fetch Reactions for Specific Emoji
let reactionsRequest = ReactionsRequestBuilder ()
. setLimit ( limit : 30 )
. setMessageId ( messageId : 148 )
. setReaction ( reaction : "👍" )
. build ()
reactionsRequest. fetchNext { reactions in
// Only returns 👍 reactions
} onError : { error in
print ( "Error: \( error ? . errorDescription ) " )
}
Fetch Previous Reactions
reactionsRequest. fetchPrevious { reactions in
for reaction in reactions {
print ( "Reaction: \( reaction. stringValue () ) " )
}
} onError : { error in
print ( "Error: \( error ? . errorDescription ) " )
}
Real-time Reaction Events
Keep the chat interactive with real-time updates for reactions.
let listenerID = "UNIQUE_LISTENER_ID"
CometChat. addMessageListener (listenerID, self )
extension YourViewController : CometChatMessageDelegate {
func onMessageReactionAdded ( reactionEvent : ReactionEvent) {
print ( "Reaction Added" )
print ( "Reaction: \( reactionEvent. reaction ) " )
print ( "Message ID: \( reactionEvent. reaction . messageId ) " )
print ( "Reacted By: \( reactionEvent. reaction . reactedBy ? . name ?? "" ) " )
}
func onMessageReactionRemoved ( reactionEvent : ReactionEvent) {
print ( "Reaction Removed" )
print ( "Reaction: \( reactionEvent. reaction ) " )
}
}
// Remove listener when done:
CometChat. removeMessageListener (listenerID)
Sample Payloads - Reaction Events
onMessageReactionAdded
onMessageReactionRemoved
Method: onMessageReactionAdded(reactionEvent: ReactionEvent)ReactionEvent Object: Parameter Type Description reaction ReactionThe reaction details receiverId StringID of the receiver receiverType ReceiverType.user or .groupconversationId StringID of the conversation parentMessageId IntParent message ID (for threads)
Method: onMessageReactionRemoved(reactionEvent: ReactionEvent)ReactionEvent Object: Parameter Type Description reaction ReactionThe reaction details receiverId StringID of the receiver receiverType ReceiverType.user or .group
Get Reactions List
To retrieve the list of reactions on a particular message:
let reactions = message. reactions // Returns [ReactionCount]
Check if Logged-in User Has Reacted
for reactionCount in message.reactions {
print ( "Reaction: \( reactionCount. reaction ) " )
print ( "Reacted by me: \( reactionCount. reactedByMe ) " )
}
Update Message With Reaction Info
When you receive a real-time reaction event, use this method to update the message with the latest reaction information. This keeps your local message state in sync with the server.
Method Signature
CometChat. updateMessageWithReactionInfo (
baseMessage : BaseMessage,
messageReaction : MessageReaction,
action : ReactionAction
) -> BaseMessage
Parameters
Parameter Type Description baseMessage BaseMessageThe message to update messageReaction MessageReactionReaction info from event action ReactionAction.REACTION_ADDED or .REACTION_REMOVED
ReactionAction Enum Values
Value Description .REACTION_ADDEDA reaction was added to the message .REACTION_REMOVEDA reaction was removed from the message
Usage Example - Reaction Added
extension YourViewController : CometChatMessageDelegate {
func onMessageReactionAdded ( reactionEvent : ReactionEvent) {
// Get the message from your local list
var message: BaseMessage = getMessageFromList (reactionEvent. reaction . messageId )
// Get the reaction from the event
let messageReaction: MessageReaction = reactionEvent. reaction
// Update the message with new reaction info
let modifiedMessage = CometChat. updateMessageWithReactionInfo (
baseMessage : message,
messageReaction : messageReaction,
action : . REACTION_ADDED
)
// Update your UI with the modified message
updateMessageInList (modifiedMessage)
}
}
Usage Example - Reaction Removed
extension YourViewController : CometChatMessageDelegate {
func onMessageReactionRemoved ( reactionEvent : ReactionEvent) {
// Get the message from your local list
var message: BaseMessage = getMessageFromList (reactionEvent. reaction . messageId )
// Get the reaction from the event
let messageReaction: MessageReaction = reactionEvent. reaction
// Update the message with removed reaction info
let modifiedMessage = CometChat. updateMessageWithReactionInfo (
baseMessage : message,
messageReaction : messageReaction,
action : . REACTION_REMOVED
)
// Update your UI with the modified message
updateMessageInList (modifiedMessage)
}
}
Complete Real-Time Handling Example
class ChatViewController : UIViewController , CometChatMessageDelegate {
var messages: [BaseMessage] = []
override func viewDidLoad () {
super . viewDidLoad ()
CometChat. addMessageListener ( "reaction-listener" , self )
}
func onMessageReactionAdded ( reactionEvent : ReactionEvent) {
handleReactionEvent (reactionEvent, action : . REACTION_ADDED )
}
func onMessageReactionRemoved ( reactionEvent : ReactionEvent) {
handleReactionEvent (reactionEvent, action : . REACTION_REMOVED )
}
private func handleReactionEvent ( _ event : ReactionEvent, action : ReactionAction) {
let messageId = event. reaction . messageId
// Find the message in local list
guard let index = messages. firstIndex ( where : { $0 . id == messageId }) else {
return
}
// Update message with reaction info
let updatedMessage = CometChat. updateMessageWithReactionInfo (
baseMessage : messages[index],
messageReaction : event. reaction ,
action : action
)
// Replace in list and refresh UI
messages[index] = updatedMessage
tableView. reloadRows ( at : [ IndexPath ( row : index, section : 0 )], with : . none )
}
deinit {
CometChat. removeMessageListener ( "reaction-listener" )
}
}
Sample Payloads - Update Message With Reaction Info
The method returns a BaseMessage with updated reactions array: Property Type Description reactions [ReactionCount]Updated list of reaction counts
Each ReactionCount contains: Property Type Description reaction StringThe emoji reaction count IntTotal count of this reaction reactedByMe BoolIf logged-in user reacted
Notes:
This is a synchronous method - no callbacks needed
Always use this method to keep local message state in sync
The returned message has updated reactions array
Works with both user and group messages
Handle both REACTION_ADDED and REACTION_REMOVED events
ReactionCount Object Properties
Property Type Description reactionStringThe reaction emoji countIntNumber of users who reacted reactedByMeBoolWhether logged-in user reacted
MessageReaction Object Properties
Property Type Description reactionStringThe reaction emoji reactedByUser?User who added the reaction reactedAtDoubleUnix timestamp when reacted messageIdIntID of the message
ReactionEvent Object Properties
Property Type Description reactionReactionThe reaction details receiverIdStringID of the receiver receiverTypeReceiverType.user or .groupconversationIdStringID of the conversation parentMessageIdIntParent message ID (for threads)
Common Error Codes
Error Code Description Resolution ERR_MESSAGE_NOT_FOUNDMessage doesn’t exist Verify message ID ERR_INVALID_REACTIONInvalid reaction emoji Use valid emoji ERR_ALREADY_REACTEDAlready reacted with emoji Remove first ERR_REACTION_NOT_FOUNDReaction not on message User hasn’t reacted