---
source_url: https://www.pubnub.com/docs/sdks/posix-cpp/api-reference/storage-and-playback
title: Message Persistence API for POSIX C++ SDK
updated_at: 2026-06-12T11:26:18.137Z
sdk_name: PubNub POSIX C++ SDK
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Message Persistence API for POSIX C++ SDK

PubNub POSIX 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](https://www.pubnub.com/docs/general/storage).

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)

## 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)

Use the following method(s) in the POSIX C++ SDK:

```cpp
history(std::string const &channel, unsigned count = 100, bool include_token = false)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channel | std::string | 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` |

### Sample code

Retrieve the last 100 messages on a channel:

```cpp
// 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([=](https://www.pubnub.com/docs/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](https://www.pubnub.com/docs/sdks/posix-cpp/api-reference/misc#time) 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:

```javascript
[
    ["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.

:::note 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

```cpp
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 | Description |
| --- | --- |
| `channel` *Type: std::string | Channel or comma-separated list of channels to get message counters for. |
| *Type: `std::vector<std::string> const&` | Vector of channels to get message counters for |
| `timetoken` *Type: std::string | Token to be used to count message since for all channels |
| `channel_timetokens` *Type: std::string | Comma-separated list of timetokens, to be used for their respective `@p` channel. Has to have the same number of elements as `@p` channel |
| *Type: `std::vector<std::string> const&` | A vector of timetokens, to be used for their respective `@p` channel. Has to have the same as `@p` channel |
| *Type: `std::vector<std::pair<std::string, std::string> > const&` | A vector of pairs: channel -> token, specifying both the channels to get message counts for and the token for each channel. |

### Sample code

```cpp
/** 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

```cpp
// Obtain message count for channels 'one' and 'two' since 15517035062228243
futres fr = pb.message_counts({"one","two"}, "15517035062228243");
```

### Example: vector of pairs

```cpp
// 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.

:::note 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`

### Sample code

```cpp
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 |

## Terms in this document

* **Message** - A unit of data transmitted between clients or between a client and a server in PubNub, containing information such as text, binary data, or structured data formats like JSON. Messages are sent over channels and can be tracked for delivery and read status.
* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.