Lua Storage & Playback API Reference for Realtime Apps
The PubNub Storage and Playback Service provides realtime 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
Requires Storage & Playback add-on Requires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function fetches historical messages of a channel.
It is possible to control how messages are returned and in what order, for example you can:
- 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
totrue
. - Page through results by providing a
start
ORend
timetoken. - Retrieve a slice of the time line by providing both a
start
ANDend
timetoken. - Limit the number of messages to a specific quantity using the
count
parameter.
Start & End parameter usage clarity:
If only the start
parameter is specified (without end
), you will receive messages that are older than and up to that start
timetoken value. If only the end
parameter is specified (without start
) you will receive messages that match that end
timetoken value and newer Specifying values for both start
and end
parameters will return messages between those timetoken values (inclusive on the end
value). Keep in mind that you will still receive a maximum of 100 messages even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the start
timetoken is necessary to page through the full set of results if more than 100 messages meet the timetoken values.
Method(s)
To run History
you can use the following method(s) in the Lua SDK:
pubnub_obj:history(params)
Parameter | Type | Required | Description |
---|---|---|---|
params | table | Yes | Table of history parameters. See History Parameters for more details. |
Properties | Type | Required | Defaults | 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). |
Using the reverse
parameter:
Messages are always returned sorted in ascending time direction from Storage/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.
Basic Usage
Retrieve the last 100 messages on a channel:
pubnub_obj:history({
channel = "demo",
count = 100,
callback = function(response)
textout(response)
end,
error = function (response)
textout(response)
end
})