Message Persistence API for C# 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)
Request execution
Use try/catch when working with the C# SDK.
If a request has invalid parameters (for example, a missing required field), the SDK throws an exception. If the request reaches the server but fails (server error or network issue), the error details are available in the returned status.
1try
2{
3 PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
4 .Message("Why do Java developers wear glasses? Because they can't C#.")
5 .Channel("my_channel")
6 .ExecuteAsync();
7
8 PNStatus status = publishResponse.Status;
9
10 Console.WriteLine("Server status code : " + status.StatusCode.ToString());
11}
12catch (Exception ex)
13{
14 Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
15}
Fetch history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Fetch historical messages from one or more channels. Use IncludeMessageActions to include message actions.
You can 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 retrieve messages between those timetokens (inclusive of theendvalue).
You can receive up to 100 messages for a single channel. For multiple channels (up to 500), you can receive up to 25 messages per channel. If more messages match the time range, make iterative calls and adjust the start timetoken to page through the results.
Method(s)
Use the following method(s) in the C# 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[] | Channels to fetch history messages from (up to 500). |
IncludeMetaType: bool | Whether to include the meta object (if provided at publish time) in the response. |
IncludeMessageTypeType: bool | Whether to include message type. Default is true. |
IncludeCustomMessageTypeType: bool | Whether to retrieve messages with the custom message type. See Retrieving Messages. |
IncludeUUIDType: bool | Whether to receive the publisher uuid. Default is true. |
IncludeMessageActionsType: bool | Retrieve history messages with message actions. If true, limited to one channel and 25 messages. Default is false. |
ReverseType: bool | Traverse from oldest to newest when set to true. |
StartType: long | Timetoken delimiting the start (exclusive) of the time slice. |
EndType: long | Timetoken delimiting the end (inclusive) of the time slice. |
MaximumPerChannelType: int | Number of historical messages to return. Default and maximum are 100 for a single channel, 25 for multiple channels, and 25 if IncludeMessageActions is true. |
QueryParamType: Dictionary <string, object> | Pass name/value pairs as query string parameters. Useful for adding custom debug information or additional parameters required by specific PubNub features. |
Execute *Type: PNCallback | PNCallback of type PNFetchHistoryResult. |
ExecuteAsyncType: None | Returns PNResult<PNFetchHistoryResult>. |
Truncated response
If truncated, a more property is returned; make iterative calls adjusting parameters.
Sample code
Reference code
Retrieve the last message on a channel:
Retrieve the last 25 messages on a channel synchronously
StartEndQueryParam<string, object>AsyncPNCallback of type PNDeleteMessageResult.This parameter is deprecated and will be removed in a future version. Please use the
ExecuteAsync parameter instead.Execute *PNCallback of type PNDeleteMessageResult.ExecuteAsyncPNResult<PNDeleteMessageResult>.Sample code
ChannelsTimetoken *timetokens, in the same order as the channels list. Specify a single timetoken to apply it to all channels; otherwise, lengths must match or the function returns a PNStatus error.QueryParam<string, object>AsyncPNMessageCountResult.This parameter is deprecated and will be removed in a future version. Please use the
ExecuteAsync parameter instead.Execute *PNMessageCountResult.ExecuteAsyncPNResult<PNMessageCountResult>.Sample code
IncludeMetaReversetrue.IncludeTimetokentimetokens in the response.StartEndCountQueryParam<string, object>AsyncPNCallback of type PNHistoryResult.This parameter is deprecated and will be removed in a future version. Please use the
ExecuteAsync parameter instead.Execute *PNCallback of type PNHistoryResult.ExecuteAsyncPNResult<PNHistoryResult>.Using the reverse parameter
Messages are always returned sorted in ascending time direction from history regardless of Reverse. The Reverse direction matters when you have more than 100 (or Count, if it's set) messages in the time interval, in which case Reverse determines the end of the time interval from which it should start retrieving the messages.
Sample code (deprecated)
Retrieve the last 100 messages on a channel: