> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-ios-ui-kit-sdk-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Messaging

> Overview of CometChat iOS SDK messaging including sending text, media, and custom messages with real-time delivery, read receipts, and threading.

<Info>
  **Quick Reference for AI Agents & Developers**

  * **Send text:** `CometChat.sendTextMessage(message:onSuccess:onError:)`
  * **Send media:** `CometChat.sendMediaMessage(message:onSuccess:onError:)`
  * **Receive messages:** `CometChat.addMessageListener("UNIQUE_ID", self)`
  * **Fetch history:** `MessagesRequest.MessageRequestBuilder().build()` → `messagesRequest.fetchPrevious(onSuccess:onError:)`
  * **Message types:** `TextMessage`, `MediaMessage`, `CustomMessage`, `InteractiveMessage` (all extend `BaseMessage`)
  * **Related:** [Send Message](/sdk/ios/send-message) · [Receive Message](/sdk/ios/receive-message) · [Typing Indicators](/sdk/ios/typing-indicators)
</Info>

Messaging is one of the core features of CometChat. We've thoughtfully created methods to help you send, receive and fetch message history.

At the minimum, you must add code for [sending messages](/sdk/ios/send-message) and [receiving messages](/sdk/ios/receive-message).

Once you've implemented that, you can proceed to more advanced features like [typing indicators](/sdk/ios/typing-indicators) and [delivery & read receipts](/sdk/ios/delivery-read-receipts).

***

## Message Data Models

All message types in CometChat inherit from the `BaseMessage` class. Understanding these data models is essential for working with messages.

### BaseMessage Properties

The `BaseMessage` class contains properties common to all message types:

| Property          | Type              | Description                                                     |
| ----------------- | ----------------- | --------------------------------------------------------------- |
| `id`              | `Int`             | Server-assigned unique message identifier                       |
| `muid`            | `String`          | Client-generated unique identifier                              |
| `senderUid`       | `String`          | UID of the message sender                                       |
| `receiverUid`     | `String`          | UID of the receiver (user or group)                             |
| `messageType`     | `MessageType`     | Type: `.text`, `.image`, `.video`, `.audio`, `.file`, `.custom` |
| `receiverType`    | `ReceiverType`    | `.user` or `.group`                                             |
| `sentAt`          | `Int`             | Unix timestamp (seconds) when sent                              |
| `deliveredAt`     | `Double`          | Unix timestamp when delivered                                   |
| `readAt`          | `Double`          | Unix timestamp when read                                        |
| `deliveredToMeAt` | `Double`          | When delivered to current user                                  |
| `readByMeAt`      | `Double`          | When read by current user                                       |
| `sender`          | `User?`           | User object of the sender                                       |
| `receiver`        | `AppEntity?`      | User or Group object                                            |
| `metaData`        | `[String: Any]?`  | Custom metadata dictionary                                      |
| `conversationId`  | `String`          | Unique conversation identifier                                  |
| `parentMessageId` | `Int`             | Parent message ID (for threads)                                 |
| `replyCount`      | `Int`             | Number of replies                                               |
| `mentionedUsers`  | `[User]`          | Array of mentioned users                                        |
| `mentionedMe`     | `Bool`            | Whether current user is mentioned                               |
| `reactions`       | `[ReactionCount]` | Reaction counts                                                 |
| `editedAt`        | `Double`          | When edited                                                     |
| `deletedAt`       | `Double`          | When deleted                                                    |

### TextMessage

Extends `BaseMessage` with:

| Property | Type        | Description      |
| -------- | ----------- | ---------------- |
| `text`   | `String`    | The text content |
| `tags`   | `[String]?` | Message tags     |

### MediaMessage

Extends `BaseMessage` with:

| Property      | Type            | Description          |
| ------------- | --------------- | -------------------- |
| `attachment`  | `Attachment?`   | Single attachment    |
| `attachments` | `[Attachment]?` | Multiple attachments |
| `caption`     | `String?`       | Caption text         |
| `tags`        | `[String]?`     | Message tags         |

### CustomMessage

Extends `BaseMessage` with:

| Property     | Type             | Description            |
| ------------ | ---------------- | ---------------------- |
| `customData` | `[String: Any]?` | Custom JSON data       |
| `type`       | `String?`        | Custom type identifier |
| `subType`    | `String?`        | Custom subtype         |

### Attachment

| Property        | Type     | Description     |
| --------------- | -------- | --------------- |
| `fileName`      | `String` | File name       |
| `fileExtension` | `String` | File extension  |
| `fileSize`      | `Double` | Size in bytes   |
| `fileMimeType`  | `String` | MIME type       |
| `fileUrl`       | `String` | URL of the file |

***

## Success & Failure Responses

### Send Message Success

When a message is sent successfully, you receive the complete message object with server-assigned properties:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    CometChat.sendTextMessage(message: textMessage, onSuccess: { (sentMessage) in
        // sentMessage.id - Server-assigned message ID
        // sentMessage.sentAt - Server timestamp
        // sentMessage.sender - Populated User object
        // sentMessage.conversationId - Conversation identifier
        print("Message ID: \(sentMessage.id)")
        print("Sent at: \(sentMessage.sentAt)")
    }, onError: { (error) in
        print("Error: \(error?.errorDescription ?? "")")
    })
    ```
  </Tab>
</Tabs>

### Common Error Codes

| Error Code                | Description               |
| ------------------------- | ------------------------- |
| `ERR_NOT_LOGGED_IN`       | User is not logged in     |
| `ERR_UID_NOT_FOUND`       | Receiver UID not found    |
| `ERR_GUID_NOT_FOUND`      | Group GUID not found      |
| `ERR_BLOCKED_BY_RECEIVER` | Blocked by receiver       |
| `ERR_NOT_A_MEMBER`        | Not a member of the group |
