Create message drafts

Create drafts of messages that can contain plain text, links, mention users, or reference channels.

Create

createMessageDraft() creates a message draft (MessageDraft object) that can consist of:

Method signature

This method has the following signature:

channel.createMessageDraft({
userSuggestionSource?: "channel" | "global",
isTypingIndicatorTriggered?: boolean,
userLimit?: number,
channelLimit?: number
}): MessageDraft

Input

ParameterTypeRequiredDefaultDescription
userSuggestionSourcechannel or globalNochannelThis parameter refers to the Mentions feature. Data source from which you want to retrieve users. You can choose either the list of channel members (channel) or users on the app's Admin Portal keyset (global).
isTypingIndicatorTriggeredbooleanNotrueThis parameter refers to the Typing Indicator feature. Defines if the typing indicator should be enabled when writing the message.
userLimitnumberNo10This parameter refers to the Mentions feature. Maximum number of usernames (name field from the User object) you can mention in one message (the default value is 10 and max is 100).
channelLimitnumberNo10This parameter refers to the References feature. Maximum number of channel names (name field from the Channel object) you can reference in one message (the default value is 10 and max is 100).

Output

TypeDescription
MessageDraftCreated MessageDraft object with the content of the message, all links, referenced channels, mentioned users and their names.

Basic usage

Create a draft message containing just plain text.

const messageDraft = channel.createMessageDraft()
messageDraft.onChange("Hi, support team!")

// message content can be accessed through "messageDraft.value"

Send

send() publishes the draft text message with all mentioned users, links, and referenced channels. Whenever you mention any users with @, send() also emits events of type mention.

The send() method depends on the sendText() method to handle the actual sending of the message.

icon

Under the hood

Method signature

This method has the following signature:

messageDraft.send({
storeInHistory?: boolean,
sendByPost?: boolean,
meta?: any,
ttl?: number,
}): Promise<{ timetoken: number }>

Input

ParameterTypeRequiredDefaultDescription
storeInHistorybooleanNotrueIf true, the messages are stored in Message Persistence (PubNub storage).
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.

Suppose a user adds elements to the message draft, such as links, quotes, other user mentions, or channel references. In that case, these are not explicitly passed in the send() method but get added to the MessageDraft object through these MessageDraft object methods:

All of these elements are sent with the rest of the message using the sendText() method.

Output

TypeDescription
Promise<{ timetoken: number }>Returned object that contains the timetoken of the message.

Basic usage

Send a draft message containing just plain text.

const messageDraft = channel.createMessageDraft()
// Content is drafted
messageDraft.onChange("Hi, support team!")
// Content is sent
messageDraft.send()

Store

Chat SDK does not provide a method for storing the drafted message on the client, so whenever a user drafts a message in a chat app, changes a channel, and goes back to that channel, the drafted message gets lost.

To save drafted messages before they are published on channels, implement your own local storage mechanism suitable for the platform you use.

Last updated on