C++Windows C++Windows C++Posix C++Windows C++ Storage & Playback API Reference for Realtime Apps

PubNub's Storage & Playback feature enables developers to store messages as they are published, and retrieve them at a later time. Our storage layer is a time-series based database. Each published message is timestamped to the nearest 10 nanoseconds.

Learn more about our Storage & Playback feature here.

This function fetches historical messages of a channel.
PubNub Storage/Playback Service 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.
To run History you can use the following method(s) in the Windows C++ SDK
  1. ParameterTypeRequiredDescription
    channelstd::string const &YesSpecifies channel to return history messages from.
    countintOptionalSpecifies the number of historical messages to return.

    default/maximum is 100.
    include_tokenBoolOptionalIf true the message post timestamps will be included in the history response.

    default: false
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;
  }
}

// Lambdas
void history(pubnub::context &ipn) {
  ipn.history("history_channel").then([=](pubnub::context &pn, pubnub_res res) {
    auto msg = pn.get_all();
 
    if (PNR_OK == res) {
      for (auto &&m: msg) {
        std::cout << m << std::endl;
      }
    } else {
      std::cout << "Request failed" << std::endl;
    }
  });
}
 
// Functions
void on_history(pubnub::context &pn, pubnub_res res) {
  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;
  }
}
 
void history(pubnub::context &pn) {
  pn.history("history_channel", 100).then(on_history);
}
An array is returned on success. The history() function returns a list of up to 100 messages, the time token of the first (oldest) message and the time token 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"
]

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.

 For keys with unlimited message retention enabled, this transaction considers only messages published in the last 7 days.
  1. 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);
    ParameterTypeRequiredDescription
    channelstd::stringYesChannel or comma-separated list of channels to get message counters for.
    std::vector<std::string> const&YesVector of channels to get message counters for
    timetokenstd::stringYesToken to be used to count message since for all channels
    channel_timetokensstd::stringYesComma-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&YesA vector of timetokens, to be used for their respective @p channel. Has to have the same as as @p channel
    std::vector<std::pair<std::string, std::string> > const&YesA vector of pairs: channel -> token, specifying both the channels to get message counts for and the token for each channel.
/** Obtain message count for
    channel 'one' since 15517035062228243 and
  channel  'two' since 15517035052228243. */
    futres fr = pb.message_counts({"one","two"}, {"15517035062228243", "15517035052228243"});
TypeValueDescription
pubnub::futresUse to get the outcome of the transaction, in the same way as all other transactions
// Obtain message count for channels 'one' and 'two' since 15517035062228243
futres fr = pb.message_counts({"one","two"}, "15517035062228243");
// avoid having different number of channels and timetokens
futres fr = pb.message_counts({{"one", "15517035062228243"}, {"two", "15517035062228243"}});

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.

 For keys with unlimited message retention enabled, this transaction considers only messages published in the last 7 days.
  1. std::map<std::string, size_t> get_channel_message_counts()

    none

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";
}
TypeValueDescription
std::map<std::string, size_t>0Map: channel (name) -> message counter