Reference channels
Channel referencing lets users mention channels in messages by typing # followed by at least three letters. Matching channel names appear as suggestions.
Requires App Context
Enable App Context for your keyset in the Admin Portal.
Generic referencing
Channel references, user mentions, and links are MessageElement instances with different MentionTarget types.
Suggestions include all channels in the app keyset (up to 100), regardless of user membership. Configure up to 100 channel references per message (default: 10) and display them as links.
Implementation is similar to user mentions and links.
Add channel references
Add channel references with # followed by at least three letters of the channel name (e.g., #Sup). Use CreateChannelMentionTarget() to create a mention target linking to a specified Channel.
Method signature
Add a channel reference by calling AddMention() with the MentionTarget of type Channel.
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 #offtopic is a channel reference (channel name is offtopic).
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void AddChannelReferenceSample();
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::AddChannelReferenceSample()
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 ChannelName = "offtopic";
13 FPubnubChatMentionTarget ChannelMentionTarget = UPubnubChatMessageDraftUtilities::CreateChannelMentionTarget(ChannelName);
14
15 MyMessageDraft->AddMention(45, ChannelName.Len() + 1, ChannelMentionTarget);
show all 18 linesRemove channel references
Remove a channel reference from a draft message with RemoveMention().
Method signature
Remove channel references by calling RemoveMention() at the exact offset where the reference starts.
Refer to the RemoveMention() method for details.
Offset value
Provide the exact position of the first character; otherwise the element is not 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 channel reference from the Hello Alex! I have sent you this link on the #offtopic channel. message where #offtopic is a channel reference.
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 channel suggestions
The message elements listener returns matching channels from your keyset for the 3-letter string typed after #.
Typing #Sup returns channels like Support or Support-Agents. 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 referenced channel whenever the draft is updated and channels 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 linesOther examples
Get channel suggestions from Chat object
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|Chat|Channel")
2void GetChannelSuggestionsSample();
3
4UFUNCTION()
5void OnGetChannelSuggestionsResponse(const FPubnubChatGetChannelSuggestionsResult& Result);
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
3void ASample_Chat::GetChannelSuggestionsSample()
4{
5
6 // Assumes Chat is a valid and initialized instance of UPubnubChat
7
8 // Get channels whose name starts with the typed text asynchronously (e.g. for channel search autocomplete)
9 FOnPubnubChatGetChannelSuggestionsResponseNative Callback;
10 // ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
11 Callback.BindUObject(this, &ASample_Chat::OnGetChannelSuggestionsResponse);
12 Chat->GetChannelSuggestionsAsync(TEXT("Lob"), Callback, 10);
13}
14
15// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
show all 23 linesGet referenced channels
Removed method
ReferencedChannels() has been removed from the Message object in the latest version of the Unreal Chat SDK. Referenced channels are now managed through MessageDraft and message elements.