Unread messages

Track unread message counts for users who reconnect after being offline.

icon

Usage in Blueprints and C++


Asynchronous and synchronous method execution

Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.

  • Asynchronous methods (Async suffix) return void and 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 Native suffix (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

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​

1Membership->GetLastReadMessageTimetoken();

Output​

TypeDescription
FStringValue 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.

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#
Type: struct
Returned object containing Result and Count.
property of FPubnubChatGetUnreadMessagesCountResult: Result#
Type: FPubnubChatOperationResult
Contains Error (bool) and ErrorMessage (FString). Check Error to determine if the operation succeeded.
property of FPubnubChatGetUnreadMessagesCountResult: Count#
Type: int
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 lines

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);
Limit#
Type: int
Default: 0
Number of objects to return in response.
Filter#
Type: 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.
Sort#
Type: FPubnubMembershipSort
Default: no default
Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated.
Page#
Type: FPubnubPage
Default: no default
Object used for pagination to define which previous or next result page you want to fetch.
property of Page: Next#
Type: FString
Default: no default
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#
Type: FString
Default: no default
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​

FPubnubChatGetUnreadMessagesCountsResult#
Type: struct
Returned object containing Result and UnreadMessagesCounts.
property of FPubnubChatGetUnreadMessagesCountsResult: Result#
Type: FPubnubChatOperationResult
Operation result with Error (bool), ErrorMessage (FString), and StepResults.
property of FPubnubChatGetUnreadMessagesCountsResult: UnreadMessagesCounts#
Type: TArray<FPubnubChatUnreadMessagesCountsWrapper>
Array of structs containing Channel, Membership, and Count.
property of UnreadMessagesCounts: Channel#
Type: UPubnubChatChannel*
Channel with unread messages.
property of UnreadMessagesCounts: Membership#
Type: UPubnubChatMembership*
Returned Membership object showing the user-channel data.
property of UnreadMessagesCounts: Count#
Type: 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

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 lines

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​

  • SetLastReadMessage()

    1Membership->SetLastReadMessage(UPubnubChatMessage* Message);
  • SetLastReadMessageTimetoken()

    1Membership->SetLastReadMessageTimetoken(FString Timetoken);
Message#
Type: UPubnubChatMessage*
Default: no default
Last read message on a given channel with the timestamp that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken.
Required by SetLastReadMessage()
Yes
Required by SetLastReadMessageTimetoken()
No
Timetoken#
Type: FString
Default: no default
Timetoken of the last read message on a given channel that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken.
Required by SetLastReadMessage()
No
Required by SetLastReadMessageTimetoken()
Yes

Output​

TypeDescription
FPubnubChatOperationResultContains 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()
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#
Type: int
Default: 0
Number of objects to return in response.
Filter#
Type: 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.
Sort#
Type: FPubnubMembershipSort
Default: no default
Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated.
Page#
Type: FPubnubPage
Default: no default
Object used for pagination to define which previous or next result page you want to fetch.
property of Page: Next#
Type: FString
Default: no default
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#
Type: FString
Default: no default
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​

FPubnubChatMarkAllMessagesAsReadResult#
Type: struct
Returned object containing Result, Page, Total, and Memberships.
property of FPubnubChatMarkAllMessagesAsReadResult: Result#
Type: FPubnubChatOperationResult
Operation result with Error (bool), ErrorMessage (FString), and StepResults.
property of FPubnubChatMarkAllMessagesAsReadResult: Page#
Type: FPubnubPage
Object used for pagination to define which previous or next result page you want to fetch.
property of Page: Next#
Type: 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.
property of Page: Prev#
Type: 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.
property of FPubnubChatMarkAllMessagesAsReadResult: Total#
Type: int
Total number of messages marked as read.
property of FPubnubChatMarkAllMessagesAsReadResult: Memberships#
Type: 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

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

Was this page useful?

Last updated on