Skip to main content

Documentation Index

Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt

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

// Edit a text message
let textMessage = TextMessage(receiverUid: "UID", text: "Updated text", receiverType: .user)
textMessage.id = 12345
CometChat.edit(message: textMessage, onSuccess: { msg in }, onError: { err in })

// Listen for real-time edits
extension VC: CometChatMessageDelegate {
  func onMessageEdited(message: BaseMessage) {
    print("Edited:", message.id, message.editedAt)
  }
}
Editing a message is straightforward. Receiving edit events has two parts:
  1. Adding a listener for real-time edits when your app is running
  2. Fetching missed edits when your app was offline

Edit a Message

Use CometChat.edit(message:) with a TextMessage or CustomMessage object. Set the message ID on the object before calling edit.
let textMessage = TextMessage(receiverUid: "cometchat-uid-2", text: "Updated message", receiverType: .user)
textMessage.id = 12345

CometChat.edit(message: textMessage, onSuccess: { (message) in
    print("Message edited: \(message)")
}, onError: { (error) in
    print("Error: \(error.errorDescription)")
})
The edited message object is returned with editedAt (timestamp) and editedBy (UID of editor) fields set. The edit() method returns a BaseMessage object (or a subclass like TextMessage).

Add/Update Tags

Use tags to update tags when editing. New tags replace existing ones.
let tags = ["pinned", "important"]
let textMessage = TextMessage(receiverUid: "cometchat-uid-2", text: "Pinned message", receiverType: .user)
textMessage.id = 12345
textMessage.tags = tags

CometChat.edit(message: textMessage, onSuccess: {...}, onError: {...})
By default, CometChat allows certain users to edit a message:
UserConversation TypeEdit Capabilities
Message SenderOne-on-oneOwn messages only
Message SenderGroupOwn messages only
Group OwnerGroupAll messages
Group ModeratorGroupAll messages

Real-time Message Edit Events

The onMessageEdited callback receives a BaseMessage object with the editedAt and editedBy fields set.
extension YourViewController: CometChatMessageDelegate {

    func onMessageEdited(message: BaseMessage) {
        print("Message edited: \(message.id)")
        print("Edited at: \(message.editedAt)")
        print("Edited by: \(message.editedBy ?? "")")

        if let textMessage = message as? TextMessage {
            print("New text: \(textMessage.text)")
        }
    }
}

// Register the delegate:
CometChat.messagedelegate = self

Missed Message Edit Events

When fetching message history, edited messages have editedAt and editedBy fields set. Additionally, an Action message is added to history indicating the edit. The Action object contains:
  • actionedited
  • actionOn — Updated message object
  • actionBy — User who edited the message
  • actionFor — Receiver (User or Group)
for message in messages {
    if message.editedAt > 0 {
        print("Message \(message.id) was edited at \(message.editedAt)")
    }

    if let actionMessage = message as? ActionMessage {
        if actionMessage.action == .messageEdited {
            print("Edit action detected")
        }
    }
}
You must be the message sender or a group admin/moderator to edit a message.

Next Steps

Delete a Message

Remove messages from conversations

Send Messages

Send text, media, and custom messages

Threaded Messages

Organize conversations with message threads

Receive Messages

Listen for incoming messages in real time