SelectPHP Storage & Playback Tutorial for Realtime Apps

These docs are for version 3.8.3 of the PHP SDK. To check out the docs for the latest version Click Here.

PubNub 4.0 for PHP is our latest and greatest! Please click here for our PubNub 4.0 for PHP docs and SDK.

PubNub 3.x for PHP will encounter End Of Life (EOL) Aug 1st, 2018. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for PHP SDK, please contact us at

Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see
PubNub's Storage and Playback feature enables developers to store messages as they are published, and retrieve them at a later time. Before using this feature it must be enabled in the PubNub Admin Console.
Being able to pull an archive of messages from storage has many applications:
  • Populate chat, collaboration and machine configuration on app load.
  • Store message streams with real-time data management.
  • Retrieve historical data streams for reporting, auditing and compliance (HIPAA, SOX, Data Protection Directive, and more).
  • Replay live events with delivery of real-time data during rebroadcasting.
As needed, specific messages can be marked "do not archive" when published to prevent archiving on a per-message basis, and storage retention time can range from 1 day to forever.
These code samples build off code defined in the Pub & Sub tutorial, so before proceeding, be sure you have completed the Pub & Sub tutorial first.
To begin, lets populate a new channel with some test publishes that we'll pull from storage using the history() method.
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
for ($i = 0; $i < 500; $i++) {
    $pubnub->publish("history_channel", "message#" . $i);

In the above example, we subscribe to history_channel, and onConnect, we'll publish a barrage of test messages to it. You should see these messages as they are published on the console.
Now that we've populated storage, we can pull from storage using the history() method call:
$history = $pubnub->history('history_channel');
The response format is

	["message1", "message2", "message3",... ],
	"Start Time Token",
	"End Time Token"
By default, history() returns the last 100 messages, first-in-first-out (FIFO). Although we specified 100 for the count, that is the default, and if we hadn't specified a count, we would have still received 100. Try this example again, but specify 5 for count to see what is returned.
Setting the reverse attribute to true will return the first 100 messages, first-in-first-out (FIFO). Try this example again, but specify true for reverse to see what is returned.
The timetoken response value is a string, representing 17-digit precision unix time (UTC). To convert PubNub's timetoken to Unix timestamp (seconds), divide the timetoken number by 10,000,000 (10^7).
Given this example, you will get the last two messages published to the channel:
$pubnub->history("history_channel", 2, null, null, null, false);
To page for the next 2, use the set the start attribute to start timetoken value, and request again with all other settings the same:
As illustrated, paging with the default reverse as true pages 2 at-a-time starting from newest, in FIFO order. If you repeat these Paging example steps with reverse as false, you will page 2 at-a-time as well, starting instead from the oldest messages, but still in FIFO order. You will know you are at the end of history when the returned start timetoken is 0.

You can call the method by passing 0 or a valid time token as the argument.
function getAllMessages($startTimeToken, $pubnub) {
    $result = $pubnub->history("history_test", 100, null, $startTimeToken );

    $msgs = $result["messages"];
    $start = $result["date_from"];
    $end = $result["date_to"];
    $count = count($msgs);

    if ($count > 0) {
        echo $count . "\n";
        echo "start: " . $start . "\n";
        echo "end: " . $end . "\n";

    if (count($msgs) == 100) {
        getAllMessages($start, $pubnub);

getAllMessages(null, $pubnub);
To pull from a slice of time, just include both start and end attributes:
$pubnub->history("my_channel", 100, null, 13827485876355504, 13827475876355504);