SelectPHP Storage & Playback API Reference 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 support@pubnub.com.

PubNub's Storage & Playback feature enables developers to store messages as they are published, and retrieve them at a later time. Our storage layer is a time-series based database. Each published message is timestamped to the nearest 10 nanoseconds.

Learn more about our Storage & Playback feature here.


Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function fetches historical messages of a channel.
PubNub Storage/Playback Service 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:
  • 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 time token.
  • Retrieve a slice of the time line by providing both a start AND end time token.
  • 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.
To run History you can use the following method(s) in the PHP SDK
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    countNumberOptionalSpecifies the number of historical messages to return.

    default/maximum is 100.
    include_tokenBooleanOptionalIf True the message post timestamps will be included in the history response.

    default: False
    startNumberOptionalTime token delimiting the start of time slice (exclusive) to pull messages from.
    endNumberOptionalTime token delimiting the end of time slice (inclusive) to pull messages from.
    reverseBooleanOptionalSetting to true will search the time line starting from the oldest end of the timeline. Default is false. If both start and end arguments are provided, reverse is ignored and messages are returned starting with the newest message.
Retrieve the last 100 messages on a channel:
$history = $pubnub->history('history_channel');
 
print_r($history['messages']);
The PHP SDK returns false on fail. An array is returned on success. The history() function returns a list of up to 100 messages, the time token of the first (oldest) message and the time token of the last (newest) message in the resulting set of messages. The output below demonstrates the format for a history() response:

[
	["message1", "message2", "message3",... ],
	"Start Time Token",
	"End Time Token"
]
  1. $pubnub->history("my_channel", 3, null, null, null, true);
    
     [
    	["Pub1","Pub2","Pub3"],
    	13406746729185766,
    	13406746780720711
    ]
    
  2. $pubnub->history("my_channel", 100, null, 13406746780720711, null, true);
    
     [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  3. $pubnub->history("my_channel", 100, null, null, 13406746780720711);
    
    [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  4.  
    Usage!
    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);