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 a message to a channel. The method handles text content, file attachments, metadata, user mentions, and channel references.

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 message: string,
3 options?: SendTextOptionParams
4 // {
5 // storeInHistory?: boolean,
6 // sendByPost?: boolean,
7 // meta?: any,
8 // ttl?: number,
9 // quotedMessage?: Message,
10 // files?: FileList | File[] | FileInput[]
11 // }
12): Promise<unknown>

Input

* required
ParameterDescription
message *
Type: string
Default:
n/a
Text that you want to send to the selected channel.
options
Type: SendTextOptionParams
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 →quotedMessage
Type: Message
Default:
n/a
Object added to a message when you quote another message. This object stores the following info about the quoted message: { timetoken: quotedMessage.timetoken, text: quotedMessage.text, userId: quotedMessage.userId }, where timetoken is the time when the quoted message was published, text contains the original message content, and userId is the identifier of the user who published the quoted message.
options →files
Type: FileList, File[], or FileInput[]
Default:
n/a
One or multiple files attached to the text message. They can take the form of a read-only files property of the HTML <input /> element (when a user has selected one or more files from their device), or through a drag-and-drop operation (FileList). Alternatively, that can be an array of files objects (File[]) or PubNub-specific SendFileParameters["file"][].

Output

TypeDescription
Promise<any>
Returned object with a value of any type.

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 message = 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