---
source_url: https://www.pubnub.com/docs/sdks/lua/api-reference/storage-and-playback
title: Storage and Playback API for Lua SDK
description: Learn how to fetch historical messages with the Storage and Playback API for Lua SDK in PubNub documentation.
updated_at: 2026-11-09T13:29:01.000Z
---

# Storage and Playback API for Lua SDK

> For AI agents: documentation index at https://www.pubnub.com/llms-full.txt

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/data-storage/message-history/overview.md).

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

:::warning Requires Message Persistence
Enable Message Persistence for your key in the [Admin Portal](https://admin.pubnub.com/). See how to [enable add-on features](https://www.pubnub.com/docs/architecture/authentication/set-up-your-account.md#enable-the-features-your-app-calls).
:::

This function fetches historical messages of a channel.

You can control how messages are returned and in what order.

* Search for messages starting on the newest end of the timeline (default behavior - `reverse` = `false`).
* Search for messages from the oldest end of the timeline by setting `reverse` to `true`.
* Page through results by providing a `start` OR `end` timetoken.
* Retrieve a slice of the timeline by providing both a `start` AND `end` timetoken.
* Limit the number of messages to a specific quantity using the `count` parameter.

:::warning How to use the start and end parameters
PubNub retrieves messages by searching **backward through time** (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:
* **start** is the **newer** boundary (higher timetoken value) - search begins here, **exclusive**
* **end** is the **older** boundary (lower timetoken value) - search stops here, **inclusive**
When you provide both parameters, `start` must be a **higher** timetoken than `end`.
```mermaid
graph LR
    A(["9:00 AM<br/>(oldest)"]) -->|"time flows →"| B["end = 10:00 AM<br/>older boundary<br/>inclusive ✓"]
    B --> C["messages<br/>retrieved"]
    C --> D["start = 11:00 AM<br/>newer boundary<br/>exclusive ✗"]
    D -->|"time flows →"| E(["11:59 AM<br/>(newest)"])
    D -.->|"◄ API searches backward"| B
    style A fill:#f8f9fa,stroke:#6c757d,color:#495057
    style E fill:#f8f9fa,stroke:#6c757d,color:#495057
    style B fill:#d4edda,stroke:#28a745,color:#000
    style C fill:#cce5ff,stroke:#004085,color:#000
    style D fill:#fff3cd,stroke:#856404,color:#000
```
9:00 AM10:00 AM11:00 AM11:59 AM
Results are always returned in oldest-first order.
:::

### Method(s)

Use the following method(s) in the Lua SDK:

```lua
pubnub_obj:history(params)
```

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| params | table | Yes | Table of `history` parameters. See [History Parameters](#history-parameters) for more details. |

#### History parameters

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| Channel | string | Yes | `none` | The channel to get history for. |
| callback | function(r) | Yes | `none` | The function to call with received history. |
| error | function(r) | Optional | `function(r) end` | The function to call on failure, with result. |
| start | integer or string | Optional | `none` | The timetoken to start history from. |
| stop | integer or string | Optional | `none` | The timetoken to stop history at. |
| reverse | boolean | Optional | `false` | Whether to get the history in reverse (from chronological) order. |
| count | integer | Optional | `10` | Maximum number of messages to get in history (cannot be more than 100). |

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

Retrieve the last 100 messages on a channel:

```lua
pubnub_obj:history({
    channel = "demo",
    count = 100,
    callback = function(response)
        textout(response)
    end,
    error = function (response)
        textout(response)
    end
})
```

Last updated at: 2026-11-09T13:29:01.000Z
