---
source_url: https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/send-receive
title: Send and receive messages
updated_at: 2026-06-29T11:45:38.280Z
---

> 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 plain text messages to a channel with lightweight delivery options. If you need to send rich content (mentions, channel references, links, files, or quoted messages), use [MessageDraft](https://www.pubnub.com/docs/chat/chat-sdk/learn/chat-entities/message-draft) instead.

:::tip Encryption available
The Chat SDK supports AES-CBC 256-bit encryption for messages and files. See [data security configuration](https://www.pubnub.com/docs/chat/chat-sdk/learn/access-control#data-security).
:::

### Method signature

##### Under the hood

`sendText()` calls Pub/Sub API and the JavaScript SDK [publish()](https://www.pubnub.com/docs/sdks/javascript/api-reference/publish-and-subscribe#publish) method and Files API through [sendFile()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#send-file) and [getFileUrl()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#get-file-url) methods.

This method takes the following parameters:

```ts
channel.sendText(
    text: string,
    options?: SendTextParams
): Promise<PublishResponse>
```

`SendTextParams` has the following shape:

```ts
type SendTextParams = {
    meta?: any
    storeInHistory?: boolean
    sendByPost?: boolean
    ttl?: number
    customPushData?: Record<string, string>
}
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| text | string | Yes |  | Text that you want to send to the selected channel. |
| options | SendTextParams | Optional |  | This object holds the configuration options. |
| options →storeInHistory | boolean | Optional | `true` | If `true`, the messages are stored in Message Persistence. If `storeInHistory` is not specified, the Message Persistence configuration specified on the Admin Portal keyset is used. |
| options →sendByPost | boolean | Optional | `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. |
| options →meta | any | Optional |  | Publish additional details with the request. |
| options →ttl | number | Optional |  | Defines if / how long (in hours) the message should be stored in Message Persistence. If storeInHistory = true, and ttl = 0, the message is stored with no expiry time., If storeInHistory = 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 storeInHistory = 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. |
| options →customPushData | Record<string, | Optional |  | Custom key-value pairs to include in the push notification payload sent to mobile devices. |

#### Output

| Type | Description |
| --- | --- |
| `Promise<PublishResponse>` | Result of the PubNub Publish call. |

:::warning Deprecated overload
The previous `sendText()` overload accepting `SendTextOptionParams` (which included `quotedMessage` and `files` as top-level options) was renamed to `sendTextLegacy()`.
`sendTextLegacy()` is deprecated and kept only to ease migration from `0.x.x`. You should use `sendText()` for new integrations, and migrate away from `sendTextLegacy()` because it will be removed in a future release.
:::

### Sample code

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.

```ts
// reference the channel where you want to send a text message
const channel = await chat.getChannel("support")
// invoke the "sendText()" method on the "channel" object
const response = await channel.sendText(
    "Hi, Everyone!",
    {
        storeInHistory: true,
        meta: {
            messageImportance: "high"
        },
        ttl: 15,
    }
)
```

## Receive

To receive messages on a given channel, you must [connect to the channel](https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/watch) and start listening to message events.