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
startparameter (withoutend), you receive messages older than thestarttimetoken. - If you specify only the
endparameter (withoutstart), you receive messages from thatendtimetoken and newer. - If you specify both
startandend, 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:
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>)
| Parameter | Description |
|---|---|
Channels *Type: string[] | Specifies channel to return history messages from. Maximum of 500 channels are allowed. |
IncludeMetaType: bool | Whether meta (passed when Publishing the message) should be included in response or not. |
IncludeMessageTypeType: bool | Pass true to receive the message type with each history message. Default is true. |
IncludeCustomMessageTypeType: bool | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
IncludeUUIDType: bool | Pass true to receive the publisher uuid with each history message. Default is true. |
IncludeMessageActionsType: 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. |
ReverseType: bool | Setting to true will traverse the time line in reverse starting with the oldest message first. |
StartType: long | timetoken delimiting the start of time slice (exclusive) to pull messages from. |
EndType: long | timetoken delimiting the end of time slice (inclusive) to pull messages from. |
MaximumPerChannelType: 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. |
QueryParamType: 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. |
ExecuteAsyncType: 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:
1
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. |
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.
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>)
| Parameter | Description |
|---|---|
Channel *Type: string | Specifies channel messages to be deleted from history. |
StartType: long | timetoken delimiting the Start of time slice (inclusive) to delete messages from. |
EndType: long | timetoken delimiting the End of time slice (exclusive) to delete messages from. |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNCallback of type PNDeleteMessageResult. |
Execute *Type: System.Action | System.Action of type PNDeleteMessageResult. |
ExecuteAsyncType: 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>)
| 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. |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNMessageCountResult. |
Execute *Type: System.Action | System.Action of type PNMessageCountResult. |
ExecuteAsyncType: None | Returns Task<PNResult<PNMessageCountResult>>. |
Sample code
1
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
1
Retrieve count of messages using different timetokens for each channel
1