On this page

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

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.

icon

Single listener


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 lines

Get mentioned users

MentionedUsers() returns all users mentioned in a message.

Method signature

Output

TypeDescription
TArray<FPubnubMentionedUser>
Array with specific mentioned users.
FPubnubMentionedUser
PropertyDescription
Id
Type: FString
Unique identifier of the mentioned user.
Name
Type: 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 lines

GetCurrentUserMentions() retrieves all instances where the current user was mentioned in channels or threads. Use this to build a mentions feed.

Method signature

Output

This method returns a FPubnubUserMentionDataList object containing two fields: UserMentions and IsMore.

ParameterDescription
UserMentions
Type: 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.
isMore
Type: 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 lines

Show 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

icon

Handle the response

Output
TypeDescription
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
Last updated on