On this page

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

Actor.h
1

Actor.cpp
1

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

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

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);
* required
ParameterDescription
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:
n/a
Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated.
Page
Type: FPubnubPage
Default:
n/a
Object used for pagination to define which previous or next result page you want to fetch.
 → Next
Type: FString
Default:
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.
 → Prev
Type: FString
Default:
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

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

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);
ParameterRequired by SetLastReadMessage()Required by SetLastReadMessageTimetoken()Description
Message
Type: 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.
Timetoken
Type: FString
Default:
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

TypeDescription
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()
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);
* required
ParameterDescription
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:
n/a
Struct defining the property to sort by, and a sort direction. Available options are id, name, and updated.
Page
Type: FPubnubPage
Default:
n/a
Object used for pagination to define which previous or next result page you want to fetch.
 → Next
Type: FString
Default:
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.
 → Prev
Type: FString
Default:
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

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

Last updated on