Skip to main content
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)")
}
Method: CometChat.addReaction(messageId:reaction:)
ParameterTypeValue
messageIdInt38230
reactionString"👍"
Request:
ParameterTypeValue
messageIdInt38230
reactionString"❤️"
Updated Reactions List:
ReactionCountReactedByMe
👍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)")
}
Method: CometChat.removeReaction(messageId:reaction:)
ParameterTypeValue
messageIdInt38230
reactionString"👍"

Fetch Reactions for a Message

To get all reactions for a specific message, create a ReactionsRequest using ReactionsRequestBuilder.
MethodDescription
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)")
}
Method: ReactionsRequest.fetchNext()
ParameterTypeValue
messageIdInt38230
limitInt30
reactionString?nil (optional - filter by emoji)

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)
Method: onMessageReactionAdded(reactionEvent: ReactionEvent)ReactionEvent Object:
ParameterTypeDescription
reactionReactionThe reaction details
receiverIdStringID of the receiver
receiverTypeReceiverType.user or .group
conversationIdStringID of the conversation
parentMessageIdIntParent message ID (for threads)

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

ParameterTypeDescription
baseMessageBaseMessageThe message to update
messageReactionMessageReactionReaction info from event
actionReactionAction.REACTION_ADDED or .REACTION_REMOVED

ReactionAction Enum Values

ValueDescription
.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")
    }
}
The method returns a BaseMessage with updated reactions array:
PropertyTypeDescription
reactions[ReactionCount]Updated list of reaction counts
Each ReactionCount contains:
PropertyTypeDescription
reactionStringThe emoji reaction
countIntTotal count of this reaction
reactedByMeBoolIf 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

PropertyTypeDescription
reactionStringThe reaction emoji
countIntNumber of users who reacted
reactedByMeBoolWhether logged-in user reacted

MessageReaction Object Properties

PropertyTypeDescription
reactionStringThe reaction emoji
reactedByUser?User who added the reaction
reactedAtDoubleUnix timestamp when reacted
messageIdIntID of the message

ReactionEvent Object Properties

PropertyTypeDescription
reactionReactionThe reaction details
receiverIdStringID of the receiver
receiverTypeReceiverType.user or .group
conversationIdStringID of the conversation
parentMessageIdIntParent message ID (for threads)

Common Error Codes

Error CodeDescriptionResolution
ERR_MESSAGE_NOT_FOUNDMessage doesn’t existVerify message ID
ERR_INVALID_REACTIONInvalid reaction emojiUse valid emoji
ERR_ALREADY_REACTEDAlready reacted with emojiRemove first
ERR_REACTION_NOT_FOUNDReaction not on messageUser hasn’t reacted