On this page

Send and receive messages

Requires Message Persistence

Enable Message Persistence on your keyset in the Admin Portal 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 instead.

Encryption available

The Chat SDK supports AES-CBC 256-bit encryption for messages and files. See data security configuration.

Method signature

icon

Under the hood


This method takes the following parameters:

1channel.sendText(
2 text: string,
3 options?: SendTextParams
4): Promise<PublishResponse>

SendTextParams has the following shape:

1type SendTextParams = {
2 meta?: any
3 storeInHistory?: boolean
4 sendByPost?: boolean
5 ttl?: number
6 customPushData?: Record<string, string>
7}

Input

* required
ParameterDescription
text *
Type: string
Default:
n/a
Text that you want to send to the selected channel.
options
Type: SendTextParams
Default:
n/a
This object holds the configuration options.
options →storeInHistory
Type: boolean
Default:
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
Type: boolean
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.
options →meta
Type: any
Default:
n/a
Publish additional details with the request.
options →ttl
Type: number
Default:
n/a
Defines if / how long (in hours) the message should be stored in Message Persistence.
  1. If storeInHistory = true, and ttl = 0, the message is stored with no expiry time.
  2. 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.
  3. If storeInHistory = false, the ttl parameter is ignored.
  4. If ttl is not specified, then the expiration of the message defaults back to the expiry value for the keyset.
options →customPushData
Type: Record<string, string>
Default:
n/a
Custom key-value pairs to include in the push notification payload sent to mobile devices.

Output

TypeDescription
Promise<PublishResponse>
Result of the PubNub Publish call.
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.

1// reference the channel where you want to send a text message
2const channel = await chat.getChannel("support")
3// invoke the "sendText()" method on the "channel" object
4const response = await channel.sendText(
5 "Hi, Everyone!",
6 {
7 storeInHistory: true,
8 meta: {
9 messageImportance: "high"
10 },
11 ttl: 15,
12 }
13)

Receive

To receive messages on a given channel, you must connect to the channel and start listening to message events.

Last updated on