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
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
| Parameter | Description |
|---|---|
message *Type: stringDefault: n/a | Text that you want to send to the selected channel. |
optionsType: SendTextOptionParamsDefault: 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 →quotedMessageType: MessageDefault: 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 →filesType: 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
| Type | Description |
|---|---|
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.