---
source_url: https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/messages/send-receive
title: Send and receive messages
updated_at: 2026-06-04T11:09:43.635Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Send and receive messages

:::note Requires Message Persistence
Enable [Message Persistence](https://youtu.be/qLMtbINWGig) on your keyset in the [Admin Portal](https://admin.pubnub.com/) to store messages.
:::

## Send

Use `sendText()` to send a [message](https://www.pubnub.com/docs/chat/swift-chat-sdk/learn/chat-entities/message) to a channel. Pass additional publishing options in a `SendTextParams` struct. To attach files, add user mentions, or quote messages, use a [MessageDraft](https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/messages/drafts) instead.

### Method signature

This method takes the following parameters:

```swift
channel.sendText(
    text: String,
    params: SendTextParams = SendTextParams()
) async throws -> Timetoken
```

The `SendTextParams` struct groups common publishing options:

```swift
public struct SendTextParams {
    public var meta: [String: JSONCodable]?
    public var shouldStore: Bool
    public var usePost: Bool
    public var ttl: Int?
    public var customPushData: [String: String]?
}
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| text | String | Yes |  | Text that you want to send to the selected channel. |
| params | SendTextParams | Optional | `SendTextParams()` | Publishing options grouped in a single struct. See below for details. |

#### SendTextParams fields

| Parameter | Description |
| --- | --- |
| `meta`Type: `[String: JSONCodable]`Default: `nil` | Publish additional details with the request. |
| `shouldStore`Type: `Bool`Default: `true` | If `true`, the messages are stored in Message Persistence. If `shouldStore` is not specified, the Message Persistence configuration specified on the Admin Portal keyset is used. |
| `usePost`Type: `Bool`Default: `false` | When `true`, the SDK uses HTTP POST to publish the messages. The message is sent in the BODY of the request instead of the query string when HTTP GET is used. The messages are also compressed to reduce their size. |
| `ttl`Type: `Int`Default: `nil` | Defines if / how long (in hours) the message should be stored in Message Persistence. If shouldStore = true, and ttl = 0, the message is stored with no expiry time., If shouldStore = true and ttl = X, the message is stored with an expiry time of X hours unless you have message retention set to Unlimited on your keyset configuration in the Admin Portal., If shouldStore = false, the ttl parameter is ignored., If ttl is not specified, then the expiration of the message defaults back to the expiry value for the keyset. |
| `customPushData`Type: `[String: String]`Default: `nil` | Custom key-value pairs to include in push notification payloads. |

#### Output

| Parameter | Description |
| --- | --- |
| `Timetoken` | Returned timetoken of the published message. |

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

Send the `Hi Everyone!` message to the `support` channel. Mark its `high` priority and state that it should be stored in Message Persistence for `15` hours.

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    let timetoken = try await channel.sendText(
      text: "Hi Everyone",
      params: SendTextParams(
        meta: ["messageImportance": "high"],
        shouldStore: true,
        ttl: 15
      )
    )
    debugPrint("Message sent successfully at \(timetoken)")
  } else {
    debugPrint("Channel not found")
  }
}
```

## Receive

To receive messages on a given channel, use [onMessageReceived()](https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/channels/watch) (closure-based) or [channel.stream.messages()](https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/channels/watch) (AsyncStream-based) to start listening to message events.