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:
- Plain text
- Files
- Mentioned users
- Referenced channels
- Links
Method signature
This method has the following signature:
channel.createMessageDraft({
userSuggestionSource?: "channel" | "global",
isTypingIndicatorTriggered?: boolean,
userLimit?: number,
channelLimit?: number
}): MessageDraft
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
userSuggestionSource | channel or global | No | channel | This 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 ). |
isTypingIndicatorTriggered | boolean | No | true | This parameter refers to the Typing Indicator feature. Defines if the typing indicator should be enabled when writing the message. |
userLimit | number | No | 10 | This 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 ). |
channelLimit | number | No | 10 | This 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
Type | Description |
---|---|
MessageDraft | Created 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.
Method signature
This method has the following signature:
messageDraft.send({
storeInHistory?: boolean,
sendByPost?: boolean,
meta?: any,
ttl?: number,
}): Promise<{ timetoken: number }>
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
storeInHistory | boolean | No | true | If 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. |
sendByPost | boolean | No | 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. |
meta | any | No | n/a | Publish additional details with the request. |
ttl | number | No | n/a | Defines if / how long (in hours) the message should be stored in Message Persistence.
|
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
Type | Description |
---|---|
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.