Create message drafts (v1)
Create message drafts containing plain text, links, user mentions, or channel references.
Message drafts functionality version
This is version 1 documentation. For the latest version, see Drafts.
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:
1channel.createMessageDraft({
2 userSuggestionSource?: "channel" | "global",
3 isTypingIndicatorTriggered?: boolean,
4 userLimit?: number,
5 channelLimit?: number
6}): MessageDraft
Input
| Parameter | Description |
|---|---|
userSuggestionSourceType: channel or globalDefault: 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). |
isTypingIndicatorTriggeredType: booleanDefault: true | This parameter refers to the Typing Indicator feature. Defines if the typing indicator should be enabled when writing the message. |
userLimitType: numberDefault: 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, the min is 1, and max is 100. |
channelLimitType: numberDefault: 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, the min is 1, 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. |
Sample code
Create a draft message containing just plain text.
1const messageDraft = channel.createMessageDraft()
2messageDraft.onChange("Hi, support team!")
3
4// message content can be accessed through "messageDraft.value"
Send
send() publishes the draft message with all mentioned users, links, and referenced channels. User mentions emit mention events.
This method uses sendText() internally.
Method signature
This method has the following signature:
1messageDraft.send({
2 storeInHistory?: boolean,
3 sendByPost?: boolean,
4 meta?: any,
5 ttl?: number,
6}): Promise<{ timetoken: number }>
Input
| Parameter | Description |
|---|---|
storeInHistoryType: booleanDefault: 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. |
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. |
metaType: anyDefault: n/a | Publish additional details with the request. |
ttlType: numberDefault: 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. |
Sample code
Send a draft message containing just plain text.
1const messageDraft = channel.createMessageDraft()
2// Content is drafted
3messageDraft.onChange("Hi, support team!")
4// Content is sent
5messageDraft.send()
Store
Chat SDK does not persist drafts. Implement your own local storage to save drafts across channel switches.