Message Persistence API for Unity SDK

Message Persistence gives you real-time access to the history of messages published to PubNub. Each message is timestamped to the nearest 10 nanoseconds and stored across multiple availability zones in several geographic locations. You can encrypt stored messages with AES-256 so they are not readable on PubNub’s network. For details, see Message Persistence.

You control how long messages are stored through your account’s retention policy. Options include: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.

You can retrieve the following:

  • Messages
  • Message reactions
  • Files (using the File Sharing API)

Fetch history​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Fetch historical messages from one or more channels. Use includeMessageActions to include message actions.

It's possible to control how messages are returned and in what order.

  • If you specify only Start, you receive messages older than that timetoken. Start is the newer boundary of the query (exclusive).
  • If you specify only End, you receive messages newer than or equal to that timetoken. End is the older boundary of the query (inclusive).
  • If you specify both Start and End, Start must be a higher timetoken than End. Results contain messages between those timetokens, excluding the message at Start and including the message at End.
How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

This function returns up to 100 messages on a single channel, or 25 per channel on up to 500 channels. To page, iteratively update the Start timetoken.

Method(s)​

Use the following method(s) in the Unity SDK:

1pubnub.FetchHistory()
2 .Channels(string[])
3 .IncludeMeta(bool)
4 .IncludeMessageType(bool)
5 .IncludeCustomMessageType(bool)
6 .IncludeUUID(bool)
7 .IncludeMessageActions(bool)
8 .Reverse(bool)
9 .Start(int)
10 .End(int)
11 .MaximumPerChannel(int)
12 .QueryParam(Dictionary<string, object>)
required
Channels required#
Type: string[]
Specifies channel to return history messages from. Maximum of 500 channels are allowed.
IncludeMeta#
Type: bool
Whether meta (passed when Publishing the message) should be included in response or not.
IncludeMessageType#
Type: bool
Pass true to receive the message type with each history message. Default is true.
IncludeCustomMessageType#
Type: bool
Indicates whether to retrieve messages with the custom message type.

For more information, refer to Retrieving Messages.
IncludeUUID#
Type: bool
Pass true to receive the publisher uuid with each history message. Default is true.
IncludeMessageActions#
Type: bool
The flag denoting to retrieve history messages with message actions. If true, the method is limited to one channel and 25 messages only. Default is false.
Reverse#
Type: bool
Setting to true will traverse the time line in reverse starting with the oldest message first.
Start#
Type: long
Newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. Must be a higher timetoken value than End when both are provided.
End#
Type: long
Older boundary of the query (inclusive). Retrieval stops at this timetoken. Must be a lower timetoken value than Start when both are provided.
MaximumPerChannel#
Type: int
Specifies the number of historical messages to return. Default and maximum is 100 for a single channel, 25 for multiple channels, and 25 if IncludeMessageActions is true.
QueryParam#
Type: Dictionary<string, object>
QueryParam accepts a Dictionary object, the keys and values are passed as the query string parameters of the URL called by the API.
Execute required#
Type: System.Action
System.Action of type PNFetchHistoryResult.
ExecuteAsync#
Type: None
Returns Task<PNResult<PNFetchHistoryResult>>.
Truncated response

If truncated, a more property will be returned with additional parameters. Make iterative calls adjusting parameters.

Sample code​

Retrieve the last message on a channel:

1

Returns​

The FetchHistory() operation returns a PNFetchHistoryResult that contains the following properties :

Property NameTypeDescription
MessagesDictionary<string, List<PNHistoryItemResult>>List of messages.
MoreMoreInfoPagination information.

The Messages has the following properties:

Property NameTypeDescription
 →  Channel NamestringName of the channel for which FetchHistory() has been executed.
 →  →  timetokenlongtimetoken associated with the message.
 →  →  EntryobjectPayload of the message.
 →  →  MetaobjectMetadata associated with the message.
 →  →  UuidstringUUID associated with the message.
 →  →  MessageTypestringMessageType associated with the message.
 →  →  CustomMessageTypestringThe custom message type associated with the message.
 →  →  ActionsobjectMessage Actions associated with the message.

The More has following properties:

Property NameTypeDescription
 →  →  Startlongtimetoken denoting the start of the requested range.
 →  →  Endlongtimetoken denoting the end of the requested range.
 →  →  LimitintNumber of messages returned in response.
1{
2 "Messages":
3 {
4 "my_channel":
5 [{
6 "Timetoken":15717278253295153,
7 "Entry":"sample message",
8 "Meta":"",
9 "Uuid":"user-1",
10 "MessageType":null,
11 "Actions":null
12 }]
13 },
14 "More":null
15}

Other examples​

Retrieve the last 25 messages on a channel synchronously​

1

Delete messages from history​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Removes the messages from the history of a specific channel.

Required setting

Enable Delete-From-History in key settings and initialize with a secret key.

How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

Method(s)​

To Delete Messages from History you can use the following method(s) in the Unity SDK.

1pubnub.DeleteMessages()
2 .Channel(string)
3 .Start(long)
4 .End(long)
5 .QueryParam(Dictionary<string,object>)
required
Channel required#
Type: string
Specifies channel messages to be deleted from history.
Start#
Type: long
timetoken delimiting the Start of time slice (inclusive) to delete messages from.
End#
Type: long
timetoken delimiting the End of time slice (exclusive) to delete messages from.
QueryParam#
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncDeprecated#
Type: PNCallback
PNCallback of type PNDeleteMessageResult.
Execute required#
Type: System.Action
System.Action of type PNDeleteMessageResult.
ExecuteAsync#
Type: None
Returns Task<PNResult<PNDeleteMessageResult>>.

Sample code​

1

Returns​

The DeleteMessages() operation returns a PNResult<PNDeleteMessageResult> which returns empty PNDeleteMessageResult object.

Other examples​

Delete messages sent in a particular timeframe​

1

Delete specific message from history​

To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. For example, if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.

1

Message counts​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Return the number of messages published since the given time; the count is messages with timetoken ≥ the provided value.

Unlimited message retention

For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.

Method(s)​

You can use the following method(s) in the Unity SDK:

1pubnub.MessageCounts()
2 .Channels(string[])
3 .ChannelsTimetoken(long[])
4 .QueryParam(Dictionary<string, object>)
required
Channels required#
Type: string[]
The channels to fetch the message count
ChannelsTimetoken required#
Type: long[]
Array of timetokens, in order of the channels list. Specify a single timetoken to apply it to all channels. Otherwise, the list of timetokens must be the same length as the list of channels, or the function returns a PNStatus with an error flag.
QueryParam#
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncDeprecated#
Type: PNCallback
PNCallback of type PNMessageCountResult.
Execute required#
Type: System.Action
System.Action of type PNMessageCountResult.
ExecuteAsync#
Type: None
Returns Task<PNResult<PNMessageCountResult>>.

Sample code​

1

Returns​

The operation returns a PNResult<PNMessageCountResult> which contains the following operations:

Property NameTypeDescription
ResultPNMessageCountResultReturns a PNMessageCountResult object.
StatusPNStatusReturns a PNStatus object

PNMessageCountResult contains the following properties:

Property NameTypeDescription
ChannelsDictionary<string, long>Collection of channels along with the messages count. channels without messages have a count of 0. channels with 10,000 messages or more have a count of 10000.

Other examples​

Retrieve count of messages for a single channel​

1

Retrieve count of messages using different timetokens for each channel​

1

Was this page useful?

Last updated on