Unread messages
Track unread message counts for users who reconnect after being offline.
Asynchronous and synchronous method execution
Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.
-
Asynchronous methods (
Asyncsuffix) returnvoidand take an optional delegate parameter that fires when the operation completes.1Chat->GetUnreadMessagesCountsAsync(OnGetUnreadMessagesCountsResponseDelegate);You can also use native callbacks that accept lambdas instead of dynamic delegates. Native callback types have the
Nativesuffix (for example,FOnPubnubChatGetUnreadMessagesCountsResponseNative). -
Synchronous methods (no suffix) block the main game thread until the operation completes and return a result struct directly.
1FPubnubChatGetUnreadMessagesCountsResult Result = Chat->GetUnreadMessagesCounts();
lastReadMessageTimetoken on the Membership object stores when a user last read messages on a channel. This is set automatically on Join() or Invite(). Update it based on user actions (scrolling, app focus, etc.) using mark as read methods.
Requires App Context and Message Persistence
Enable App Context and Message Persistence in the Admin Portal.
Get last read message
GetLastReadMessageTimetoken() returns the timetoken marking the user's last read position on a channel. This timetoken doesn't always correspond to an actual message. Use it to display unread markers in your UI.
Method signature
- C++ / Input parameters
- Blueprint
1Membership->GetLastReadMessageTimetoken();
Output
| Type | Description |
|---|---|
FString | Value of the returned timetoken. |
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.
Get the last-read message timetoken from a membership.
- C++
- Blueprint
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMembership")
2void GetLastReadMessageTimetokenSample();
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
3void ASample_ChatMembership::GetLastReadMessageTimetokenSample()
4{
5
6 // Assumes Membership is a valid UPubnubChatMembership
7
8 // Get last-read message timetoken from local cache
9 FString LastReadTimetoken = Membership->GetLastReadMessageTimetoken();
10}
Get unread messages count (one channel)
GetUnreadMessagesCount() returns the number of unread messages on a channel. This counts all messages after the last read timetoken, including your own messages.
Method signature
This method has the following signature:
1Membership->GetUnreadMessagesCount();
Output
FPubnubChatGetUnreadMessagesCountResult#- Returned object containing
ResultandCount. - property of FPubnubChatGetUnreadMessagesCountResult:
Result# - Contains
Error(bool) andErrorMessage(FString). CheckErrorto determine if the operation succeeded. - property of FPubnubChatGetUnreadMessagesCountResult:
Count# - The number of unread messages on the channel for this membership.
Sample code
Get the unread messages count for a membership.
1
2// ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
3void ASample_ChatMembership::GetUnreadMessagesCountSample()
4{
5
6 // Assumes Membership is a valid UPubnubChatMembership
7
8 // Callback for when the operation completes (returns unread count)
9 FOnPubnubChatGetUnreadMessagesCountResponseNative Callback;
10 // ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
11 Callback.BindUObject(this, &ASample_ChatMembership::OnGetUnreadMessagesCountResponse);
12 Membership->GetUnreadMessagesCountAsync(Callback);
13}
14
15// ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
show all 20 linesGet unread messages count (all channels)
GetUnreadMessagesCounts() returns unread counts for all joined channels with unread messages (channels with zero unread are excluded). Counts include all messages after the last read timetoken, including your own.
Unread counts and filtering
Filters support channel.* fields plus status, type, custom (not uuid.*). Unread counts include your own messages. See Memberships filters.
Method signature
1Chat->GetUnreadMessagesCounts(
2 int Limit = 0,
3 FString Filter = "",
4 FPubnubMembershipSort Sort = FPubnubMembershipSort(),
5 FPubnubPage Page = FPubnubPage()
6);
Limit#- Number of objects to return in response.
Filter#- Filter expression evaluated against channel memberships only. Allowed targets:
channel.*plus common fields (status,type,custom).uuid.*fields aren't supported. Sort#- Struct defining the property to sort by, and a sort direction. Available options are
id,name, andupdated. Page#- Object used for pagination to define which previous or next result page you want to fetch.
- property of Page:
Next# - Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
- property of Page:
Prev# - Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the
Nextparameter is supplied.
Output
FPubnubChatGetUnreadMessagesCountsResult#- Returned object containing
ResultandUnreadMessagesCounts. - property of FPubnubChatGetUnreadMessagesCountsResult:
Result# - Operation result with
Error(bool),ErrorMessage(FString), andStepResults. - property of FPubnubChatGetUnreadMessagesCountsResult:
UnreadMessagesCounts# - Array of structs containing
Channel,Membership, andCount. - property of UnreadMessagesCounts:
Channel# - Channel with unread messages.
- property of UnreadMessagesCounts:
Membership# - Returned
Membershipobject showing the user-channel data. - property of UnreadMessagesCounts:
Count# - Total number of messages unread by the current user on a given channel.
Sample code
Get the number of all messages unread by the support_agent_15 user on all joined channels.
1
2// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
3void ASample_Chat::GetUnreadMessagesCountsSample()
4{
5
6 // Assumes Chat is a valid and initialized instance of UPubnubChat
7
8 // Get unread message counts per channel (e.g. for badge counts on channel list)
9 FOnPubnubChatGetUnreadMessagesCountsResponseNative Callback;
10 // ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
11 Callback.BindUObject(this, &ASample_Chat::OnGetUnreadMessagesCountsResponse);
12 Chat->GetUnreadMessagesCountsAsync(Callback, 0);
13}
14
15// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
show all 23 linesTo avoid counting your own recently sent messages as unread, ensure your app updates the last read timetoken.
Mark messages as read (one channel)
SetLastReadMessage() and SetLastReadMessageTimetoken() set the last read timetoken for counting unread messages. Bind these to user actions in your app.
Setting the last read message enables Read Receipts to track which member read which message. For non-public channels, these methods also emit a Receipt chat event so other clients can track read position.
Method signature
- C++ / Input parameters
- Blueprint
-
SetLastReadMessage()1Membership->SetLastReadMessage(UPubnubChatMessage* Message); -
SetLastReadMessageTimetoken()1Membership->SetLastReadMessageTimetoken(FString Timetoken);
Message#- Last read message on a given channel with the timestamp that gets added to the user-channel membership as the
customparameter calledlastReadMessageTimetoken.- Required by SetLastReadMessage()
- Yes
- Required by SetLastReadMessageTimetoken()
- No
Timetoken#- Timetoken of the last read message on a given channel that gets added to the user-channel membership as the
customparameter calledlastReadMessageTimetoken.- Required by SetLastReadMessage()
- No
- Required by SetLastReadMessageTimetoken()
- Yes
Output
| Type | Description |
|---|---|
FPubnubChatOperationResult | Contains Error (bool) and ErrorMessage (FString). Check Error to determine if the operation succeeded. The membership is updated in place. |
Sample code
Set the last-read message timetoken for a membership.
SetLastReadMessageTimetoken()
- C++
- Blueprint
1
2// ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
3void ASample_ChatMembership::SetLastReadMessageTimetokenSample()
4{
5
6 // Assumes Membership is a valid UPubnubChatMembership
7
8 // Timetoken of the message to mark as last read (17-digit PubNub timetoken)
9 FString Timetoken = TEXT("17388000000000000");
10
11 // Set last-read message timetoken (no result callback needed)
12 Membership->SetLastReadMessageTimetokenAsync(Timetoken, nullptr);
13}
SetLastReadMessage()
1
2// ACTION REQUIRED: Replace ASample_ChatMembership with name of your Actor class
3void ASample_ChatMembership::SetLastReadMessageSample()
4{
5
6 // Assumes Membership is a valid UPubnubChatMembership
7
8 // Message to mark as last read (e.g. from channel history)
9 UPubnubChatMessage* Message = nullptr;
10
11 // Set last-read message using a message object (no result callback needed)
12 Membership->SetLastReadMessageAsync(Message, nullptr);
13}
Mark messages as read (all channels)
MarkAllMessagesAsRead() marks all unread messages as read on all joined channels.
Method signature
This method has the following signature:
1Chat->MarkAllMessagesAsRead(
2 int Limit = 0,
3 FString Filter = "",
4 FPubnubMembershipSort Sort = FPubnubMembershipSort(),
5 FPubnubPage Page = FPubnubPage()
6);
Limit#- Number of objects to return in response.
Filter#- Filter expression evaluated against channel memberships only. Allowed targets:
channel.*plus common fields (status,type,custom).uuid.*fields aren't supported. Sort#- Struct defining the property to sort by, and a sort direction. Available options are
id,name, andupdated. Page#- Object used for pagination to define which previous or next result page you want to fetch.
- property of Page:
Next# - Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
- property of Page:
Prev# - Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the
Nextparameter is supplied.
Output
FPubnubChatMarkAllMessagesAsReadResult#- Returned object containing
Result,Page,Total, andMemberships. - property of FPubnubChatMarkAllMessagesAsReadResult:
Result# - Operation result with
Error(bool),ErrorMessage(FString), andStepResults. - property of FPubnubChatMarkAllMessagesAsReadResult:
Page# - Object used for pagination to define which previous or next result page you want to fetch.
- property of Page:
Next# - Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
- property of Page:
Prev# - Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the
nextparameter is supplied. - property of FPubnubChatMarkAllMessagesAsReadResult:
Total# - Total number of messages marked as read.
- property of FPubnubChatMarkAllMessagesAsReadResult:
Memberships# - Array of all related memberships.
Sample code
Mark the total number of 50 messages as read and specify you want to fetch the results from the next page using a string that was previously returned from the PubNub server.
1
2// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
3void ASample_Chat::MarkAllMessagesAsReadSample()
4{
5
6 // Assumes Chat is a valid and initialized instance of UPubnubChat
7
8 // Mark all messages as read for the current user's memberships
9 FOnPubnubChatMarkAllMessagesAsReadResponseNative Callback;
10 // ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
11 Callback.BindUObject(this, &ASample_Chat::OnMarkAllMessagesAsReadResponse);
12 Chat->MarkAllMessagesAsReadAsync(Callback, 0);
13}
14
15// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
show all 20 lines