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
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
| Parameter | Description |
|---|---|
text *Type: stringDefault: n/a | Text that you want to send to the selected channel. |
optionsType: SendTextParamsDefault: n/a | This object holds the configuration options. |
options →storeInHistoryType: booleanDefault: 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 →sendByPostType: booleanDefault: 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 →metaType: anyDefault: n/a | Publish additional details with the request. |
options →ttlType: numberDefault: n/a | Defines if / how long (in hours) the message should be stored in Message Persistence.
|
options →customPushDataType: Record<string, string>Default: n/a | 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. |
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.