Mention users

Tag users in chat messages with @ mentions. Type @ followed by at least three letters to see username suggestions.

icon

Usage in Blueprints and C++


Requires App Context

Enable App Context in the Admin Portal to mention users.

Shared structure

Channel references, user mentions, and links are MessageElement instances with different MentionTarget types.

Configuration options:

  • User source: channel members or all app users
  • Suggestions: up to 100 usernames (default: 10)
  • Username length: up to 200 characters
  • Mentions per message: up to 100 (default: 10)

Implementation follows similar patterns to channel referencing and links.

Add user mentions​

AddMention() adds user mentions to a draft message. Use CreateUserMentionTarget() to create a mention target linking to a specified User.

Method signature​

Add a user mention by calling AddMention() with the MentionTarget of type User.

Refer to the AddMention() method for details.

Sample code​

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

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

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void AddUserMentionSample();
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::AddUserMentionSample()
4{
5
6 // Assumes Channel is a valid UPubnubChatChannel (e.g. from GetChannel)
7
8 UPubnubChatMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
9
10 MyMessageDraft->InsertText(0, "Hello Alex! I have sent you this link on the #offtopic channel.");
11
12 FString UserName = "Alex";
13 FPubnubChatMentionTarget UserMentionTarget = UPubnubChatMessageDraftUtilities::CreateUserMentionTarget(UserName);
14
15 MyMessageDraft->AddMention(6, UserName.Len(), UserMentionTarget);
show all 18 lines

Remove user mentions​

RemoveMention() removes a user mention from a draft message.

Method signature​

Remove a user mention by calling RemoveMention() at the exact offset where the mention starts.

Refer to the RemoveMention() method for details.

Offset value

Provide the exact offset position where the mention starts; otherwise, it won't be removed.

Sample code​

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Remove the user mention from the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention.

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void MessageDraftRemoveMentionSample();
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::MessageDraftRemoveMentionSample()
4{
5
6 // Assumes MessageDraft is a valid UPubnubChatMessageDraft with existing mentions
7 // e.g. the message reads: "Hello Alex! I have sent you this link on the #offtopic channel."
8
9 // Remove the link mention at position 33
10 MessageDraft->RemoveMention(33);
11}

Get user suggestions​

The OnMessageDraftUpdatedWithSuggestions delegate returns users matching a 3-letter string from channel members or global users. Bind this delegate to receive draft changes along with suggestions for user mentions, links, and channel references.

icon

Details


Example: typing @Ale returns users starting with Ale like Alex or Alexander. Default: 10 suggestions (max: 100).

Method signature​

Refer to the Listen for message draft changes (with suggestions) section for details.

Sample code​

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Insert the first mentioned user whenever the draft is updated and users are detected.

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void MessageDraftInsertSuggestedMentionSample();
3
4void OnMessageDraftUpdateWithSuggestions_InsertSample(const TArray<FPubnubChatMessageElement>& MessageElements, const TArray<FPubnubChatSuggestedMention>& SuggestedMentions);
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::MessageDraftInsertSuggestedMentionSample()
4{
5
6 // Assumes Channel is a valid UPubnubChatChannel (e.g. from GetChannel)
7
8 UPubnubChatMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
9
10 // Bind to receive suggestions and automatically insert the first one
11 MyMessageDraft->OnMessageDraftUpdatedWithSuggestionsNative.AddUObject(this, &ASample_ChatMessageDraft::OnMessageDraftUpdateWithSuggestions_InsertSample);
12
13 // Trigger the delegate and suggestions
14 MyMessageDraft->Update("Please coordinate with @Al for the meeting.");
15}
show all 27 lines

Other examples​

Get user suggestions from Chat object​

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|Chat|User")
2void GetUserSuggestionsSample();
3
4UFUNCTION()
5void OnGetUserSuggestionsResponse(const FPubnubChatGetUserSuggestionsResult& Result);
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
3void ASample_Chat::GetUserSuggestionsSample()
4{
5
6 // Assumes Chat is a valid and initialized instance of UPubnubChat
7
8 // Bind a callback with signature matching FOnPubnubChatGetUserSuggestionsResponseNative, then call Async
9 FOnPubnubChatGetUserSuggestionsResponseNative Callback;
10 // ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
11 Callback.BindUObject(this, &ASample_Chat::OnGetUserSuggestionsResponse);
12 Chat->GetUserSuggestionsAsync(TEXT("Win"), Callback, 10);
13}
14
15// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
show all 23 lines

Get mentioned users​

Removed method

MentionedUsers() has been removed from the Message object in the latest version of the Unreal Chat SDK. Mentioned users are now managed through MessageDraft and message elements. To monitor mentions, use the OnMentioned delegate on the User object or StreamMentions().

Removed method

GetCurrentUserMentions() has been removed from the Chat object in the latest version of the Unreal Chat SDK. To monitor mentions, use the OnMentioned delegate or StreamMentions(), or GetEventsHistory() to retrieve historical mention events.

Show notifications for mentions​

Use the OnMentioned delegate on the User object to receive real-time mention events. Bind the delegate and call StreamMentions() to start receiving mention events targeted to the user. Call StopStreamingMentions() to stop.

DelegateTypePayload
OnMentionedFOnPubnubChatUserMentionedFPubnubChatUserMention containing mention details (channel ID, message timetoken, etc.).

Sample code​

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatUser")
2void StreamMentionsSample();
3
4void OnUserMentioned(const FPubnubChatUserMention& UserMention);
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_ChatUser with name of your Actor class
3void ASample_ChatUser::StreamMentionsSample()
4{
5
6 // Assumes User is a valid UPubnubChatUser (e.g. from GetCurrentUser)
7
8 // Bind to receive mention notifications for this user
9 User->OnMentionedNative.AddUObject(this, &ASample_ChatUser::OnUserMentioned);
10
11 // Start streaming mentions
12 User->StreamMentionsAsync(nullptr);
13
14 // When mention notifications are no longer needed, stop streaming
15 User->StopStreamingMentionsAsync(nullptr);
show all 22 lines

Emit user mention​

Programmatically emit a mention event on a channel with EmitUserMention(). Use this when implementing custom mention logic outside of MessageDraft, for example when processing messages that arrive from external systems.

Method signature​

1Channel->EmitUserMention(
2 const FString UserID,
3 const FString Timetoken,
4 const FString Text
5);
required
UserID required#
Type: FString
Default: no default
ID of the user being mentioned.
Timetoken required#
Type: FString
Default: no default
Timetoken of the message that contains the mention.
Text required#
Type: FString
Default: no default
Content of the message containing the mention.

Output​

TypeDescription
FPubnubChatOperationResultContains Error and ErrorMessage. Check Error to determine if the event was emitted successfully.

Sample code​

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Emit a mention event for a user on a channel.

Actor.h​
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatChannel")
2void EmitUserMentionSample();
Actor.cpp​
1

2// ACTION REQUIRED: Replace ASample_ChatChannel with name of your Actor class
3void ASample_ChatChannel::EmitUserMentionSample()
4{
5
6 // Assumes Channel is a valid UPubnubChatChannel (e.g. from GetChannel)
7
8 // User ID being mentioned
9 FString UserID = TEXT("user-abc123");
10 // Timetoken of the message that contains the mention (17-digit PubNub timetoken)
11 FString Timetoken = TEXT("17388000000000000");
12 // Mention text to display (e.g. @username)
13 FString Text = TEXT("@johndoe");
14
15 // Emit a mention event for @mention notifications
show all 17 lines

Was this page useful?

Last updated on