Message Persistence API for PubNub PHP SDK
The PubNub Message Persistence Service provides real-time 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 Message Persistence add-on Requires that the Message Persistence 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 PHP V4 SDK:
$pubnub->history()->channel(String)->reverse(bool)->includeTimetoken(bool)->start(integer)->end(integer)->count(integer)->sync();
Parameter Type Required Defaults Description channel
String Yes Specifies channel
to return history messages from.reverse
Boolean Optional false
Setting to true
will traverse the time line in reverse starting with the oldest message first.includeTimetoken
Boolean Optional false
Whether event dates timetokens should be included in response or not. start
Integer Optional Timetoken delimiting the start of time slice (exclusive) to pull messages from. end
Integer Optional Timetoken delimiting the end of time slice (inclusive) to pull messages from. count
Integer Optional Specifies the number of historical messages to return.
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.
Basic Usage
Retrieve the last 100 messages on a channel:
$result = $pubnub->history()
->channel("history_channel")
->count(100)
->sync();
Response
The history()
operation returns a PNHistoryResult
which contains the following operations:
Method | Type | Description |
---|---|---|
getMessages() | Array | array of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details. |
getStartTimetoken() | Integer | Start timetoken. |
getEndTimetoken() | Integer | End timetoken. |
Method | Type | Description |
---|---|---|
getTimetoken() | Integer | Timetoken of the message. |
getEntry() | Object | Message. |
Other Examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse:
$pubnub->history() ->channel("my_channel") ->count(3) ->reverse(true) ->sync();
PubNub\Models\Consumer\History\PNHistoryResult Object( [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array( [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 11 [b] => 22 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111 ) [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 33 [b] => 44 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 55 [b] => 66 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) ) [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766 [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711 )
Use history() to retrieve messages newer than a given timetoken by paging from oldest message to newest message starting at a single point in time (exclusive):
$pubnub->history() ->channel("my_channel") ->start(13847168620721752) ->reverse(true) ->sync();
PubNub\Models\Consumer\History\PNHistoryResult Object( [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array( [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array ( [a] => 11 [b] => 22 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111 ) [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 33 [b] => 44 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 55 [b] => 66 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) ) [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766 [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711 )
Use history() to retrieve messages until a given timetoken by paging from newest message to oldest message until a specific end point in time (inclusive):
$pubnub->history() ->channel("my_channel") ->count(100) ->start(-1) ->end(13847168819178600) ->reverse(true) ->sync();
PubNub\Models\Consumer\History\PNHistoryResult Object( [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array( [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 11 [b] => 22 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111 ) [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 33 [b] => 44 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object( [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array( [a] => 55 [b] => 66 ) [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222 ) ) [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766 [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711 )
Include timetoken in history response:
$pubnub->history() ->channel("my_channel") ->count(100) ->includeTimetoken(true) ->sync();
Delete Messages from History
Requires Message Persistence add-on Requires that the Message Persistence 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
Removes the messages from the history of a specific channel.
Note
There is a setting to accept delete from history requests for a key, which you must enable by checking the Enable Delete-From-History
checkbox in the key settings for your key in the Admin Portal.
Requires Initialization with secret key.
Method(s)
To Delete Messages from History
you can use the following method(s) in the PHP V4 SDK.
$pubnub->deleteMessages()->channel(String)->start(integer)->end(integer)->sync()
Parameter Type Required Defaults Description channel
String Yes Specifies channels
to delete messages from.start
Integer Optional Timetoken delimiting the start
of time slice (inclusive) to delete messages from.end
Integer Optional Timetoken delimiting the end
of time slice (exclusive) to delete messages from.
Basic Usage
$pubnub->deleteMessages()
->channel("ch")
->start(123)
->end(456)
->sync();
Other Examples
Delete specific message from history
To delete a specific message, pass the
publish timetoken
(received from a successful publish) in theEnd
parameter andtimetoken +/- 1
in theStart
parameter. e.g. if15526611838554310
is thepublish timetoken
, pass15526611838554309
inStart
and15526611838554310
inEnd
parameters respectively as shown in the following code snippet.$pubnub->deleteMessages() ->channel("ch") ->start(15526611838554310) ->end(15526611838554309) ->sync();
Message Counts
Requires Message Persistence add-on Requires that the Message Persistence 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
Returns the number of messages published on one or more channels since a given time. The count
returned is the number of messages in history with a timetoken
value greater than or equal to
than the passed value in the channelsTimetoken
parameter.
Note
For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.
Method(s)
You can use the following method(s) in the PHP V4 SDK:
$pubnub->messageCounts()->channels(array)->channelsTimetoken(array)
Parameter Type Required Defaults Description channels
Array Yes The channels
to fetch the message countchannelsTimetoken
Array Yes Array of timetokens
, in order of the channels list. Specify a singletimetoken
to apply it to all channels. Otherwise, the list oftimetokens
must be the same length as the list of channels, or the function returns aPNStatus
with an error flag.
Basic Usage
$response = $pubnub->messageCounts()
->channels(["mychannel"])
->channelsTimetoken(["15513576173381797"])
->sync();
print_r($response->getChannels());
Returns
The operation returns a PNMessageCountsResult
which contains the following operations
Method | Type | Description |
---|---|---|
getChannels() | Array | An associative array with channel name as key and messages count as value. Channels without messages have a count of 0. Channels with 10,000 messages or more have a count of 10000. |