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.
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
| Parameter | Description |
|---|---|
FPubnubChatGetUnreadMessagesCountResultType: struct | Returned object containing Result and Count. |
→ ResultType: FPubnubChatOperationResult | Contains Error (bool) and ErrorMessage (FString). Check Error to determine if the operation succeeded. |
→ CountType: int | The number of unread messages on the channel for this membership. |
Sample code
Get the unread messages count for a membership.
1
Get 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);
| Parameter | Description |
|---|---|
LimitType: intDefault: 0 | Number of objects to return in response. |
FilterType: FString (Memberships filter)Default: "" | Filter expression evaluated against channel memberships only. Allowed targets: channel.* plus common fields (status, type, custom). uuid.* fields aren't supported. |
SortType: FPubnubMembershipSortDefault: n/a | Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated. |
PageType: FPubnubPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: FStringDefault: n/a | 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. |
→ PrevType: FStringDefault: n/a | 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 Next parameter is supplied. |
Output
| Parameter | Description |
|---|---|
FPubnubChatGetUnreadMessagesCountsResultType: struct | Returned object containing Result and UnreadMessagesCounts. |
→ ResultType: FPubnubChatOperationResult | Operation result with Error (bool), ErrorMessage (FString), and StepResults. |
→ UnreadMessagesCountsType: TArray<FPubnubChatUnreadMessagesCountsWrapper> | Array of structs containing Channel, Membership, and Count. |
→ ChannelType: UPubnubChatChannel* | Channel with unread messages. |
→ MembershipType: UPubnubChatMembership* | Returned Membership object showing the user-channel data. |
→ CountType: int | 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
To 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);
| Parameter | Required by SetLastReadMessage() | Required by SetLastReadMessageTimetoken() | Description |
|---|---|---|---|
MessageType: UPubnubChatMessage*Default: n/a | Yes | No | Last read message on a given channel with the timestamp that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken. |
TimetokenType: FStringDefault: n/a | No | Yes | Timetoken of the last read message on a given channel that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken. |
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
SetLastReadMessage()
1
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);
| Parameter | Description |
|---|---|
LimitType: intDefault: 0 | Number of objects to return in response. |
FilterType: FString (Memberships filter)Default: "" | Filter expression evaluated against channel memberships only. Allowed targets: channel.* plus common fields (status, type, custom). uuid.* fields aren't supported. |
SortType: FPubnubMembershipSortDefault: n/a | Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated. |
PageType: FPubnubPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: FStringDefault: n/a | 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. |
→ PrevType: FStringDefault: n/a | 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 Next parameter is supplied. |
Output
| Parameter | Description |
|---|---|
FPubnubChatMarkAllMessagesAsReadResultType: struct | Returned object containing Result, Page, Total, and Memberships. |
→ ResultType: FPubnubChatOperationResult | Operation result with Error (bool), ErrorMessage (FString), and StepResults. |
→ PageType: FPubnubPage | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: FString | 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. |
→ PrevType: FString | 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 next parameter is supplied. |
→ TotalType: int | Total number of messages marked as read. |
→ MembershipsType: TArray<UPubnubChatMembership*> | 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