Message Persistence API for PubNub POSIX C++ SDK
Message Persistence provides real-time access to the history of all messages published to PubNub. Each published message is timestamped to the nearest 10 nanoseconds and is stored across multiple availability zones in several geographical locations. Stored messages can be encrypted with AES-256 message encryption, ensuring that they are not readable while stored on PubNub's network. For more information, refer to Message Persistence.
Messages can be stored for a configurable duration or forever, as controlled by the retention policy that is configured on your account. The following options are available: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message actions
- File Sharing (using File Sharing API)
History
This function fetches historical messages of a channel. Message Persistence provides real-time access to an unlimited history for all messages published to PubNub. Stored messages are replicated across multiple availability zones in several geographical data center locations. Stored messages can be encrypted with AES-256 message encryption ensuring that they are not readable while stored on PubNub's network. It is possible to control how messages are returned and in what order, for example you can:
- Limit the number of messages to a specific quantity using the
count
parameter.
Method(s)
To run History
you can use the following method(s) in the Posix C++ SDK:
history(std::string const &channel, unsigned count = 100, bool include_token = false)
Parameter | Type | Required | Description |
---|---|---|---|
channel | std::string const & | Yes | Specifies channel to return history messages from. |
count | int | Optional | Specifies the number of historical messages to return. default/maximum is 100. |
include_token | Bool | Optional | If true the message post timestamps will be included in the history response. default: false |
Basic Usage
Retrieve the last 100 messages on a channel:
// Sync
void history(pubnub::context &pn) {
enum pubnub_res res;
res = pn.history("history_channel", 100).await();;
if (PNR_OK == res) {
std::vector<std::string> msg = pn.get_all();
for (std::vector<std::string>::iterator it = msg.begin(); it != msg.end(); ++it) {
std::cout << *it << std::endl;
}
} else {
std::cout << "History request failed" << std::endl;
}
show all 48 linesRest Response from Server
An array is returned on success.
The history()
function returns a list of up to 100 messages, the timetoken of the first (oldest) message and the timetoken of the last (newest) message in the resulting set of messages. The output below demonstrates the format for a history()
response:
[
["message1", "message2", "message3",... ],
"Start Time Token",
"End Time Token"
]
Message Counts
Message Counts transaction
Starts the transaction pubnub_message_counts
on the context list of channels @p
channel for messages counts starting from @p
timeoken as a single timetoken, or @p
channel_timetokens
as a separate timetoken for each channel. The message counts are the number of messages published on one or more channels since a given time.
Unlimited message retention
For keys with unlimited message retention enabled, this transaction considers only messages published in the last 30 days.
Declaration of all overloads
futres message_counts(std::string const& channel, std::string const& timetoken);
futres message_counts(std::vector<std::string> const& channel,
std::string const& timetoken);
futres message_counts(std::string const& channel,
std::vector<std::string> const& channel_timetokens);
futres message_counts(std::vector<std::string> const& channel,
std::vector<std::string> const& channel_timetokens);
futres message_counts(
std::vector<std::pair<std::string, std::string> > const& channel_timetokens);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
channel | std::string | Yes | Channel or comma-separated list of channels to get message counters for. |
std::vector<std::string> const& | Yes | Vector of channels to get message counters for | |
timetoken | std::string | Yes | Token to be used to count message since for all channels |
channel_timetokens | std::string | Yes | Comma-separated list of timetokens, to be used for their respective @p channel. Has to have the same number of elements as @p channel |
std::vector<std::string> const& | Yes | A vector of timetokens, to be used for their respective @p channel. Has to have the same as @p channel | |
std::vector<std::pair<std::string, std::string> > const& | Yes | A vector of pairs: channel -> token, specifying both the channels to get message counts for and the token for each channel. |
Basic Usage
/** Obtain message count for
channel 'one' since 15517035062228243 and
channel 'two' since 15517035052228243. */
futres fr = pb.message_counts({"one","two"}, {"15517035062228243", "15517035052228243"});
Returns
Type | Value | Description |
---|---|---|
pubnub::futres | Use to get the outcome of the transaction, in the same way as all other transactions |
Example: Single token for all channels
// Obtain message count for channels 'one' and 'two' since 15517035062228243
futres fr = pb.message_counts({"one","two"}, "15517035062228243");
Example: vector of pairs
// avoid having different number of channels and timetokens
futres fr = pb.message_counts({{"one", "15517035062228243"}, {"two", "15517035062228243"}});
Read all channel message counts
Returns all the channel message counts from the response to message_counts()
.
Message counts are returned as a map, from channel to count. The message counts are the number of messages published on one or more channels since a given time.
Unlimited message retention
For keys with unlimited message retention enabled, this transaction considers only messages published in the last 30 days.
Declaration
std::map<std::string, size_t> get_channel_message_counts()
Parameters
none
Basic Usage
auto ch_msg_count = pb.get_channel_message_counts();
for (auto const& i : ch_msg_count) {
std::cout << "Channel " << i.first << ", count: " << i.second << "\n";
}
Returns
Type | Value | Description |
---|---|---|
std::map<std::string, size_t> | 0 | Map: channel (name) -> message counter |