PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

posix-CPP

  • Getting Started
  • API Reference

    • Configuration
    • Publish & Subscribe
    • Presence
    • Access Manager
    • Channel Groups
    • Message Persistence
    • Miscellaneous
  • Status Events
  • Troubleshooting
  • Change Log
  • Feature Support
  • Platform Support

Message Persistence API for PubNub POSIX C++ SDK

The PubNub Message Persistence Service provides real-time access to history for 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 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.

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, 3, 5, 7, 15, or 30 days, and Forever.

History

Description

This function fetches historical messages of a channel. PubNub Message Persistence 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.

Method(s)

To run History you can use the following method(s) in the Posix C++ SDK:

  1. history(std::string const &channel, unsigned count = 100, bool include_token = false)
    
    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

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;
  }
}

// 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);
}

Rest 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

Description

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.

Note

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

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.

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

TypeValueDescription
pubnub::futresUse 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

Description

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.

Note

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

  1. 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

TypeValueDescription
std::map<std::string, size_t>0Map: channel (name) -> message counter
← Channel GroupsMiscellaneous →
  • History
    • Description
    • Method(s)
    • Basic Usage
    • Rest Response from Server
  • Message Counts
    • Message Counts transaction
    • Description
    • Declaration of all overloads
    • Parameters
    • Basic Usage
    • Returns
    • Example: Single token for all channels
    • Example: vector of pairs
    • Read all channel message counts
    • Description
    • Declaration
    • Parameters
    • Basic Usage
    • Returns
© PubNub Inc. - Privacy Policy