Storage and Playback API for Lua 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.
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
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
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
reversetotrue. - Page through results by providing a
startORendtimetoken. - Retrieve a slice of the timeline by providing both a
startANDendtimetoken. - Limit the number of messages to a specific quantity using the
countparameter.
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:
startis the newer boundary (higher timetoken value) - search begins here, exclusiveendis the older boundary (lower timetoken value) - search stops here, inclusive
When you provide both parameters, start must be a higher timetoken than end.
Results are always returned in oldest-first order.
Method(s)
Use the following method(s) in the Lua SDK:
1pubnub_obj:history(params)
| Parameter | Description |
|---|---|
params *Type: table | Table of history parameters. See History Parameters for more details. |
History parameters
| Parameter | Description |
|---|---|
Channel *Type: string Default: none | The channel to get history for. |
callback *Type: function(r) Default: none | The function to call with received history. |
errorType: function(r) Default: function(r) end | The function to call on failure, with result. |
startType: integer or string Default: none | The timetoken to start history from. |
stopType: integer or string Default: none | The timetoken to stop history at. |
reverseType: boolean Default: false | Whether to get the history in reverse (from chronological) order. |
countType: integer Default: 10 | Maximum number of messages to get in history (cannot be more than 100). |
tip
reverse parameterMessages 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:
1pubnub_obj:history({
2 channel = "demo",
3 count = 100,
4 callback = function(response)
5 textout(response)
6 end,
7 error = function (response)
8 textout(response)
9 end
10})