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 the
start
parameter (withoutend
), you receive messages older than thestart
timetoken. - If you specify only the
end
parameter (withoutstart
), you receive messages from thatend
timetoken and newer. - If you specify both
start
andend
, you receive messages between those timetokens (inclusive ofend
).
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:
pubnub.FetchHistory()
.Channels(string[])
.IncludeMeta(bool)
.IncludeMessageType(bool)
.IncludeCustomMessageType(bool)
.IncludeUUID(bool)
.IncludeMessageActions(bool)
.Reverse(bool)
.Start(int)
.End(int)
.MaximumPerChannel(int)
.QueryParam(Dictionary<string, object>)
Parameter | Description |
---|---|
Channels *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 | timetoken delimiting the start of time slice (exclusive) to pull messages from. |
End Type: long | timetoken delimiting the end of time slice (inclusive) to pull messages from. |
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 *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
Reference code
Retrieve the last message on a channel:
Returns
The FetchHistory()
operation returns a PNFetchHistoryResult
that contains the following properties :
Property Name | Type | Description |
---|---|---|
Messages | Dictionary<string, List<PNHistoryItemResult>> | List of messages. |
More | MoreInfo | Pagination information. |
The Messages
has the following properties:
Property Name | Type | Description |
---|---|---|
→ Channel Name | string | Name of the channel for which FetchHistory() has been executed. |
→ → timetoken | long | timetoken associated with the message. |
→ → Entry | object | Payload of the message. |
→ → Meta | object | Metadata associated with the message. |
→ → Uuid | string | UUID associated with the message. |
→ → MessageType | string | MessageType associated with the message. |
→ → CustomMessageType | string | The custom message type associated with the message. |
→ → Actions | object | Message Actions associated with the message. |
The More
has following properties:
Property Name | Type | Description |
---|---|---|
→ → Start | long | timetoken denoting the start of the requested range. |
→ → End | long | timetoken denoting the end of the requested range. |
→ → Limit | int | Number of messages returned in response. |
{
"Messages":
{
"my_channel":
[{
"Timetoken":15717278253295153,
"Entry":"sample message",
"Meta":"",
"Uuid":"user-1",
"MessageType":null,
"Actions":null
}]
},
"More":null
}
Other examples
Retrieve the last 25 messages on a channel synchronously
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.
Method(s)
To Delete Messages from History
you can use the following method(s) in the Unity SDK.
pubnub.DeleteMessages()
.Channel(string)
.Start(long)
.End(long)
.QueryParam(Dictionary<string,object>)
Parameter | Description |
---|---|
Channel *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. |
Async Type: | PNCallback of type PNDeleteMessageResult . |
Execute *Type: System.Action | System.Action of type PNDeleteMessageResult . |
ExecuteAsync Type: None | Returns Task<PNResult<PNDeleteMessageResult>> . |
Sample code
Returns
The DeleteMessages()
operation returns a PNResult<PNDeleteMessageResult>
which returns empty PNDeleteMessageResult
object.
Other examples
Delete messages sent in a particular timeframe
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.
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:
pubnub.MessageCounts()
.Channels(string[])
.ChannelsTimetoken(long[])
.QueryParam(Dictionary<string, object>)
Parameter | Description |
---|---|
Channels *Type: string[] | The channels to fetch the message count |
ChannelsTimetoken *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. |
Async Type: | PNMessageCountResult . |
Execute *Type: System.Action | System.Action of type PNMessageCountResult . |
ExecuteAsync Type: None | Returns Task<PNResult<PNMessageCountResult>> . |
Sample code
Returns
The operation returns a PNResult<PNMessageCountResult>
which contains the following operations:
Property Name | Type | Description |
---|---|---|
Result | PNMessageCountResult | Returns a PNMessageCountResult object. |
Status | PNStatus | Returns a PNStatus object |
PNMessageCountResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Channels | Dictionary<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
Retrieve count of messages using different timetokens for each channel