Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

// Add a reaction
CometChat.addReaction(messageId: 148, reaction: "\u{1F60A}") { msg in } onError: { err in }

// Remove a reaction
CometChat.removeReaction(messageId: 148, reaction: "\u{1F60A}") { msg in } onError: { err in }

// Fetch reactions for a message
let request = ReactionsRequestBuilder().setMessageId(messageId: 148).setLimit(limit: 10).build()
request.fetchNext { reactions in } onError: { error in }

// Listen for reaction events (CometChatMessageDelegate)
func onMessageReactionAdded(reactionEvent: ReactionEvent) { }
func onMessageReactionRemoved(reactionEvent: ReactionEvent) { }
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)")
}
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. Both addReaction() and removeReaction() return a BaseMessage object with the updated reactions.
CometChat.removeReaction(messageId: 148, reaction: "😴") { message in
    print("Reactions: \(message.getReactions())")
} onError: { error in
    print("Error: \(error?.errorDescription)")
}

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)")
}

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)

Get Reactions List

To retrieve the list of reactions on a particular message. Returns an array of ReactionCount objects:
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")
    }
}
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

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