Quick Reference for AI Agents & Developers
- Edit message:
CometChat.edit(message:onSuccess:onError:) - Listen for edits:
onMessageEdited(_:)in message listener delegate - Missed edits: Use
MessagesRequestwith appropriate filters - Related: Delete Message · Send Message · Messaging Overview
- Adding a listener to receive real-time message edits when your app is running
- Calling a method to retrieve missed message edits when your app was not running
Edit a Message
In other words, as a sender, how do I edit a message? In order to edit a message, you can use theeditMessage() method. This method takes an object of the BaseMessage class. At the moment, you are only allowed to edit TextMessage and CustomMessage. Thus, the BaseMessage object must either be a Text or a Custom Message.
- Swift
onSuccess() callback method of the listener. The message object will contain the editedAt field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The editedBy field is also set to the UID of the user who edited the message.
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
CometChat.edit(message:)Object Type: TextMessage (before editing)| Parameter | Type | Value |
|---|---|---|
| id | Int | 12345 |
| receiverUid | String | "cometchat-uid-2" |
| text | String | "Updated message" |
| receiverType | CometChat.ReceiverType | 0 (.user) |
Add/Update Tags
While editing a message, you can update the tags associated with the Message. You can use thetags property to do so. The tags added while editing a message will replace the tags set when the message was sent.
- Swift
Tags added while editing REPLACE existing tags on the message.
Sample Payloads - Edit with Tags
Sample Payloads - Edit with Tags
| User Role | Conversation Type | Edit Capabilities |
|---|---|---|
| Message Sender | One-on-one | Own messages only |
| Message Sender | Group | Own messages only |
| Group Owner | Group | All messages |
| Group Moderator | Group | All messages |
Real-time Message Edit Events
In other words, as a recipient, how do I know when someone edits their message in real-time? To receive real-time edit events, implementCometChatMessageDelegate:
- Swift
Sample Payloads - onMessageEdited
Sample Payloads - onMessageEdited
- Callback Payload
Method:
onMessageEdited(message:)Object Type: BaseMessage| Parameter | Type | Description |
|---|---|---|
| message | BaseMessage | The edited message object |
| message.id | Int | Message ID |
| message.editedAt | Double | Unix timestamp when edited |
| message.editedBy | String? | UID of user who edited |
| message.text | String | New text (for TextMessage) |
| message.customData | [String: Any]? | New data (for CustomMessage) |
Missed Message Edit Events
In other words, as a recipient, how do I know when someone edited their message when my app was not running? When you retrieve the list of previous messages, for the message that was edited, theeditedAt and the editedBy fields will be set. Also, for example, the total number of messages for a conversation is 100, and the message with message ID 50 was edited. Now the message with id 50 will have the editedAt and the editedBy fields set whenever it is pulled from the history. Also, the 101st message will be an Action message informing you that the message with id 50 has been edited.
When your app was not running, edited messages appear in two ways:
- Edited Message in History: The message object has
editedAtandeditedByfields set - Action Message: An
ActionMessageis added to history indicating the edit
- Swift
Sample Payloads - Action Message for Edit
Sample Payloads - Action Message for Edit
- Action Message Payload
Object Type: ActionMessage
| Parameter | Type | Description |
|---|---|---|
| action | String | "edited" |
| actionOn | BaseMessage | Updated message with edited details |
| actionBy | User | User who edited the message |
| actionFor | AppEntity | Receiver (User or Group) |
In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.
Success & Failure Responses
Edit Message Success Response
Whenedit() is successful, the onSuccess callback returns the edited BaseMessage object:
- Swift
Edit Message Failure Response
Whenedit() fails, the onError callback returns a CometChatException:
- Swift
Edited Message Properties
When a message is edited, these properties are set:| Property | Type | Description |
|---|---|---|
editedAt | Double | Unix timestamp when message was edited |
editedBy | String? | UID of user who edited the message |
updatedAt | Double | Same as editedAt for edited messages |
Common Error Codes
| Error Code | Description | Resolution |
|---|---|---|
ERR_NOT_LOGGED_IN | User is not logged in | Login first using CometChat.login() |
ERR_MESSAGE_NOT_FOUND | Message does not exist | Verify the message ID is correct |
ERR_PERMISSION_DENIED | No permission to edit | Only sender or group admin/moderator can edit |
ERR_INVALID_MESSAGE_TYPE | Cannot edit this type | Only TextMessage and CustomMessage can be edited |