Send and receive messages

Requires Message Persistence

To store data about messages, you must enable Message Persistence for your app's keyset in the Admin Portal and set the desired message retention period.

Send

Send a message to a previously defined channel using the sendText() method.

sendText() prepares the final message content for publishing, including text, any attached files, metadata, mentioned users, or referenced channels (defined in and passed from the message draft).

Encrypt/Decrypt messages and files

Chat SDK supports the PubNub's AES-CBC 256-bit crypto module used to automatically encrypt and decrypt messages and files. Learn how to configure your Chat SDK instance to enable the module in your app.

Method signature

icon

Under the hood


This method takes the following parameters:

channel.sendText(
message: string,
{
storeInHistory?: boolean,
sendByPost?: boolean,
meta?: any,
ttl?: number,
mentionedUsers?: MessageMentionedUsers,
referencedChannels?: MessageReferencedChannels,
textLinks?: TextLink[],
quotedMessage?: Message,
files?: FileList | File[] | FileInput[]
}
): Promise<any>

Input

ParameterTypeRequiredDefaultDescription
messagestringYesn/aText that you want to send to the selected channel.
storeInHistorybooleanNotrueIf 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.
sendByPostbooleanNofalseWhen 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.
metaanyNon/aPublish additional details with the request.
ttlnumberNon/aDefines 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.
  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.
mentionedUsersMessageMentionedUsersNon/aObject mapping a mentioned user (with name and ID) with the number of mention (nameOccurrenceIndex) in the message: export type MessageMentionedUsers = { [nameOccurrenceIndex: number]: { id: string; name: string } }. For example, { 0: { id: 123, name: "Mark" }, 2: { id: 345, name: "Rob" } } means that Mark will be shown on the first mention in the message and Rob on the third.
referencedChannelsMessageReferencedChannelsNon/aObject mapping the referenced channel (with name and ID) with the place where this reference (nameOccurrenceIndex) was mentioned in the message: export type MessageReferencedChannels = { [nameOccurrenceIndex: number]: { id: string; name: string } }. For example, { 0: { id: 123, name: "Support" }, 2: { id: 345, name: "Off-topic" } } means that Support will be shown on the first reference in the message and Off-topic on the third. Refer to Reference channels for more details.
textLinksTextLinkNon/aReturned array of text links that are shown as text in the message.
quotedMessageMessageNon/aObject 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.
filesFileList, File[], or FileInput[]Non/aOne 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 FileInput (FileInput[]).

Output

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

Basic usage

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.

// 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 message = 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 and start listening to message events.

Last updated on