Mention users
Tag users in chat messages with @ mentions. Type @ followed by at least three letters to see username suggestions.
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
Create the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention.
1// Create a message draft
2UPubnubMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
3
4// Insert the whole text to the message draft
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// Send the message draft with the user mention
15MyMessageDraft->Send();
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
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.
1// Assume the message reads
2// Hello Alex! I have sent you this link on the #offtopic channel.`
3// Remove the user reference
4MessageDraft->RemoveMention(6)
Get user suggestions
AddChangeListenerWithSuggestions() returns users matching a 3-letter string from channel members or global users. Add a callback to listen for draft changes and receive suggestions for user mentions, links, and channel references.
Example: typing #Sup returns channels starting with Sup like Support or Support-Agents. Default: 10 suggestions (max: 100).
Method signature
Refer to the Add a message draft listener section for details.
Sample code
Insert the first mentioned user whenever the draft is updated and users are detected.
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 // Check if there are any suggested mentions
7 if (SuggestedMentions.Num() == 0)
8 {
9 return;
10 }
11
12 // Iterate over suggested mentions to find a user reference
13 for (const FPubnubSuggestedMention& Suggestion : SuggestedMentions)
14 {
15 // Check if the suggestion corresponds to a user reference
show all 32 linesGet mentioned users
MentionedUsers() returns all users mentioned in a message.
Method signature
- Blueprint
- C++ / Input parameters
1Message->MentionedUsers()
Output
| Type | Description |
|---|---|
TArray<FPubnubMentionedUser> | Array with specific mentioned users. |
FPubnubMentionedUser
| Property | Description |
|---|---|
IdType: FString | Unique identifier of the mentioned user. |
NameType: FString | Name of the mentioned user. |
Sample code
Check if the last message on the support channel contains any mentions.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9UPubnubChannel* Channel = Chat->GetChannel("support");
10
11// Fetch historical messages
12TArray<UPubnubMessage*> Messages = Channel->GetHistory();
13
14UPubnubMessage* Message = Messages.Messages[0];
15
show all 16 linesCollect all user-related mentions
GetCurrentUserMentions() retrieves all instances where the current user was mentioned in channels or threads. Use this to build a mentions feed.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->GetCurrentUserMentions(FString StartTimetoken, FString EndTimetoken, int Count)
| Parameter | Description |
|---|---|
StartTimetokenType: FStringDefault: n/a | Timetoken delimiting the start of a time slice (exclusive) to pull messages with mentions from. For details, refer to the Fetch History section. |
EndTimetokenType: FStringDefault: n/a | Timetoken delimiting the end of a time slice (inclusive) to pull messages with mentions from. For details, refer to the Fetch History section. |
CountType: intDefault: 100 | Number of historical messages with mentions to return in a single call. Since each call returns all attached message reactions by default, the maximum number of returned messages is 100. For more details, refer to the description of the IncludeMessageActions parameter in the Unreal SDK docs. |
Output
This method returns a FPubnubUserMentionDataList object containing two fields: UserMentions and IsMore.
| Parameter | Description |
|---|---|
UserMentionsType: TArray<FPubnubUserMentionData> | Array listing the requested number of historical mention events with a set of information that differ slightly depending on whether you were mentioned in the main (parent) channel or in a thread. For mentions in the parent channel, the returned information includes these fields: ChannelID where you were mentioned, UserID that mentioned you, Event (of type mention), and Message that included the mention. For mentions in threads, the returned information includes similar fields, the only difference is that you'll get ParentChannelID and ThreadChannelID fields instead of just ChannelId to clearly differentiate the thread that included the mention from the parent channel in which this thread was created. |
isMoreType: bool | Info whether there are more historical events to pull. |
Sample code
List the last ten mentions for the current chat user.
1#include "PubnubChatSubsystem.h"
2
3UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(ContextObject);
4UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
5
6UPubnubChat* Chat = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
7
8// Define the conversation/channel ID
9FString ChannelID = "ask-support";
10
11// Define the channel data
12FPubnubChatChannelData ChannelData;
13ChannelData.ChannelName = "ask-support";
14ChannelData.Description = "Space dedicated to answering all support-related questions";
15
show all 31 linesShow notifications for mentions
ListenForEvents() monitors mention events in channels and threads you're a member of. Use this to trigger pop-up notifications.
Events documentation
See Chat events for details on mention event types.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->ListenForEvents(
2 FString ChannelID,
3 EPubnubChatEventType ChatEventType,
4 FOnPubnubEventReceived EventCallback
5);
| Parameter | Description |
|---|---|
ChannelID *Type: FStringDefault: user.id | Channel to listen for new mention events. In the case of mention events, this channel is always the current user's ID. |
ChatEventTypeType: EPubnubChatEventTypeDefault: n/a | Type of events. PCET_MENTION is the type defined for all mention events |
EventCallback *Type: FOnPubnubEventReceivedDefault: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed whenever an mention event type is detected on the specified channel. |
EPubnubChatEventType
| Value | Description |
|---|---|
PCET_TYPING | Indicates a user is typing a message. Displayed as Typing. |
PCET_REPORT | Represents an event where a message has been flagged or reported for offensive content. Displayed as Report. |
PCET_RECEIPT | Confirms receipt of a message or event. Displayed as Receipt. |
PCET_MENTION | Indicates that a user has been mentioned in a message. Displayed as Mention. |
PCET_INVITE | Represents an invitation event typically sent to a specific user. Displayed as Invite. |
PCET_CUSTOM | Custom event type for specialized behavior or use cases. Displayed as Custom. |
PCET_MODERATION | Represents an event related to content moderation actions. Displayed as Moderation. |
Output
| Type | Description |
|---|---|
UPubnubCallbackStop* | Object on which you can call Stop() to stop receiving updates. |
Sample code
Print a notification for a mention of the administrator chat user on the support channel.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9UPubnubChannel* Channel = Chat->GetChannel("support");
10
11FSendTextParams SendTextParams;
12FPubnubMentionedUser MentionedUser({"admin_george", "Administrator"});
13TMap<int, FPubnubMentionedUser> MentionedUsers;
14MentionedUsers.Add(1, MentionedUser);
15SendTextParams.MentionedUsers = MentionedUsers;
show all 28 lines