On this page

Create message drafts

MessageDraft represents an unpublished message. Use it to:

Display message elements (mentions, references, links) in your UI by adding a message draft change listener.

Message drafts consist of MessageElement objects: Text (simple strings) or MentionTarget (user mentions, channel references, URLs).

Each MentionTarget has a MentionType enum property. Available types:

  • MentionTarget::user
  • MentionTarget::channel
  • MentionTarget::url
Store draft messages locally

Unreal Chat SDK does not persist drafts. Implement your own local storage to save drafts across channel switches.

Create a draft message

CreateMessageDraft() creates a (MessageDraft object) that can consist of:

Method signature

Output

ParameterDescription
UPubnubMessageDraft*
Type: UPubnubMessageDraft*
Pointer to the created MessageDraft object, which can be used to edit the draft, insert mentions, and eventually send the message.

Sample code

Create a draft message containing just plain text.

1// Assuming you have a valid UPubnubChannel* pointer named Channel
2
3// Define a configuration for the message draft if needed (using default configuration here)
4FPubnubMessageDraftConfig DraftConfig; // This might include settings like initial content or behaviors
5
6// Create a message draft from the channel with the specified configuration
7UPubnubMessageDraft* MyMessageDraft = Channel->CreateMessageDraft(DraftConfig);
8
9// Now MyMessageDraft can be used to build and send messages
10MyMessageDraft->InsertText(0, "Hello there! Please review and confirm.");
11MyMessageDraft->Send();

Add message draft change listener

Add a callback to listen for draft content changes.

icon

Single listener


Method signature

Output

This method doesn't return any data.

Sample code

Add the listener to your message draft.

1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Define your callback function to handle updates
4void AMyActor::OnMessageDraftUpdate(const TArray<UPubnubMessageElement*>& MessageElements)
5{
6 // Iterate through the current message elements and handle updates
7 for (const UPubnubMessageElement* Element : MessageElements)
8 {
9 // Process each element as needed
10 // Example: Output the text of each element to the log
11 UE_LOG(LogTemp, Log, TEXT("Draft Element Text: %s"), *Element->GetText());
12 }
13}
14
15// Set up the delegate to bind your callback
show all 23 lines

Add message draft change listener (with suggestions)

Add a callback to listen for draft changes and receive suggestions for user mentions, links, and channel references.

icon

Single listener


Example: typing #Sup returns channels like Support or Support-Agents. Default: 10 suggestions (max: 100).

Method signature

Output

This method doesn't return any data.

Sample code

Add the listener to your message draft.

1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Define your callback function to handle updates with suggestions
4void AMyActor::OnMessageDraftUpdateWithSuggestions(const TArray<UPubnubMessageElement*>& MessageElements, const TArray<FPubnubSuggestedMention>& SuggestedMentions)
5{
6 // Iterate through the current message elements and handle updates
7 for (const UPubnubMessageElement* Element : MessageElements)
8 {
9 // Process each element as needed
10 UE_LOG(LogTemp, Log, TEXT("Draft Element Text: %s"), *Element->GetText());
11 }
12
13 // Process suggested mentions
14 for (const FPubnubSuggestedMention& Suggestion : SuggestedMentions)
15 {
show all 29 lines

Add message element

AddMention() adds a user mention, channel reference or a link specified by a mention target at a given position.

Method signature

Output

This method returns no output data.

Sample code

Create the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention, link is a URL, and #offtopic is a channel reference.

1// Create a message draft
2UPubnubMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
3
4// Insert greeting text before the mention of Alex
5MyMessageDraft->InsertText(0, "Hello Alex! I have sent you this link on the #offtopic channel.");
6
7// Create a user mention target for Alex
8FString UserName = "Alex";
9UPubnubMentionTarget* UserMentionTarget = UPubnubMentionTarget::CreateUserMentionTarget(UserName);
10
11// Add the user mention for Alex
12MyMessageDraft->AddMention(6, UserName.Len(), UserMentionTarget); // 'Hello ' is 6 characters long
13
14// Create a URL mention target for the link
15FString Url = "https://www.example.com";
show all 31 lines

Remove message element

RemoveMention() removes a user mention, channel reference, or a link at a given position.

Method signature

Output

This method returns no output data.

Sample code

Remove the URL element from the word link in the Hello Alex! I have sent you this link on the #offtopic channel. message.

1// Assume the message reads:
2// Hello Alex! I have sent you this link on the #offtopic channel.
3
4// Remove the link mention
5MessageDraft->RemoveMention(33);

Update message text

Update() replaces the text of a draft message with new content.

Removing message elements

The SDK preserves message elements when possible. If element text is modified, that element is removed.

Method signature

Output

This method returns no output data.

Sample code

Change the message I sent Alex this picture. to I did not send Alex this picture. where Alex is a user mention.

1// the message reads:
2// I sent [Alex] this picture.
3// where [Alex] is a user mention
4MessageDraft->Update("I did not send Alex this picture.");
5// the message now reads:
6// I did not send [Alex] this picture.
7// the mention is preserved because its text wasn't changed
Mention text changes

If you decided to change the name Alex to some other name, the mention would be removed because your updated text's index coincided with an existing mention.

For more manual control over inserting and removing parts of a message, refer to Insert message text and Remove message text.

Insert suggested message element

Inserts a message element returned by the MessageDraft change listener into the MessageDraft.

Text must match

The SuggestedMention must be up to date with the message text, that is, SuggestedMention.ReplaceFrom must match the message draft at position SuggestedMention.ReplaceFrom, otherwise an exception is thrown.

Method signature

Output

This method returns no output data.

Sample code

Register a listener and insert a suggested element.

1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Define your callback function that handles draft updates with suggestions
4void AMyActor::OnMessageDraftUpdateWithSuggestions(const TArray<UPubnubMessageElement*>& MessageElements, const TArray<FPubnubSuggestedMention>& SuggestedMentions)
5{
6 // Check if there are any suggested mentions
7 if (SuggestedMentions.Num() > 0)
8 {
9 // Insert the first suggested mention into the draft
10 const FPubnubSuggestedMention& FirstSuggestion = SuggestedMentions[0];
11 MyMessageDraft->InsertSuggestedMention(FirstSuggestion, FirstSuggestion.ReplaceTo);
12
13 // Log the action for debugging purposes
14 UE_LOG(LogTemp, Log, TEXT("Inserted suggested mention: %s"), *FirstSuggestion.ReplaceTo);
15 }
show all 26 lines

Insert message text

InsertText() lets you insert plain text in the draft message at a given position from the beginning of the message.

Removing message elements

If the position of the text you want to insert corresponds to an existing message element, this element is removed.

Method signature

Output

This method returns no output data.

Sample code

In the message Check this support article https://www.support-article.com/., add the word out between the words Check and this.

1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Start by setting the initial text in the message draft
4MyMessageDraft->Update(TEXT("Check this support article https://www.support-article.com/."));
5
6// Find the position to insert "out"
7// "Check " is 6 characters long, so "out" should be inserted at position 6
8int InsertPosition = 6;
9
10// Use InsertText to add the word "out" at the specified position
11MyMessageDraft->InsertText(InsertPosition, TEXT("out "));
12
13// At this point, the draft should read: "Check out this support article https://www.support-article.com/."
14
15// You may Log the resulting draft for clarity
show all 16 lines

Remove message text

RemoveText() lets you remove plain text from the draft message at a given position from the beginning of the message.

Removing message elements

If the position of the text you want to remove corresponds to an existing message element, this element is removed.

Method signature

Output

This method returns no output data.

Sample code

In the message Check out this support article https://www.support-article.com/., remove the word out.

1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Start by setting the initial text in the message draft
4MyMessageDraft->Update(TEXT("Check out this support article https://www.support-article.com/."));
5
6// Determine the position and length of the text to remove
7// "Check " is 6 characters long, so "out" starts at position 6 and has a length of 4 (including the trailing space)
8int RemovePosition = 6;
9int RemoveLength = 4;
10
11// Use RemoveText to delete the word "out " from the draft
12MyMessageDraft->RemoveText(RemovePosition, RemoveLength);
13
14// At this point, the draft should read: "Check this support article https://www.support-article.com/."
15
show all 17 lines

Send a draft message

Send() publishes the draft message with all mentioned users, links, and referenced channels. Mentioning users also emits mention events.

Method signature

Output

This method returns no output data.

Sample code

Send a draft message containing just plain text.

1// Create a message draft
2var messageDraft = Channel->CreateMessageDraft();
3
4// Add initial text
5MessageDraft->Update("Hello Alex!");
6
7// Send the message
8MessageDraft->Send();
Last updated on