Message Persistence API for PHP 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)

Fetch history

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.

Fetch historical messages from one or more channels. Use includeMessageActions to include message actions.

You can control how messages are returned and in what order.

  • If you specify only the start parameter (without end), you receive messages older than the start timetoken.
  • If you specify only the end parameter (without start), you receive messages from that end timetoken and newer.
  • If you specify both start and end, you retrieve messages between those timetokens (inclusive of the end value).

You can receive up to 100 messages for a single channel. For multiple channels (up to 500), you can receive up to 25 messages per channel. If more messages match the time range, make iterative calls and adjust the start timetoken to page through the results.

Method(s)

Use the following method(s) in the PHP SDK:

$pubnub.fetchMessages()
->channels(string|Array<string>)
->maximumPerChannel(Int)
->start(string)
->end(string)
->includeMessageActions(Boolean)
->includeMeta(Boolean)
->includeMessageType(Boolean)
->includeCustomMessageType(Boolean)
->includeUuid(Boolean)
* required
ParameterDescription
channels *
Type: string or Array<string>
Default:
n/a
Channels to fetch history messages from (up to 500).
maximumPerChannel
Type: Int
Default:
25 or 100
Number of historical messages to return. Default and maximum are 100 (single), 25 (multi), and 25 with includeMessageActions.
start
Type: string
Default:
n/a
Timetoken delimiting the start (exclusive) of the time slice.
end
Type: string
Default:
n/a
Timetoken delimiting the end (inclusive) of the time slice.
includeMessageActions
Type: Boolean
Default:
False
Whether to retrieve history messages with message actions. If True, limited to one channel and 25 messages.
includeMeta
Type: Boolean
Default:
False
Whether to include the meta object (if provided at publish time) in the response.
includeMessageType
Type: Boolean
Default:
n/a
Whether to include message type. See Retrieving Messages.
includeCustomMessageType
Type: Boolean
Default:
n/a
Whether to include the custom message type. See Retrieving Messages.
includeUuid
Type: Boolean
Default:
n/a
Whether to receive the publisher uuid.

Sample code

Retrieve the last message on a channel:

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Returns

The fetchMessages() operation returns an PNFetchMessagesResult which contains the following fields:

PNFetchMessagesResult

MethodDescription
channels
Type: Array
Array of PNFetchMessageItem
startTimetoken
Type: Int
Start timetoken.
endTimetoken
Type: Int
End timetoken.

PNFetchMessageItem

MethodDescription
message
Type: string
The message
meta
Type: Any
Meta value
messageType
Type: Any
Type of the message
customMessageType
Type: Any
Custom type of the message
uuid
Type: string
UUID of the sender
timetoken
Type: Int
Timetoken of the message
actions
Type: List
A 3-dimensional List of message actions, grouped by action type and value

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.

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 timetoken.
  • Retrieve a slice of the time line by providing both a start AND end 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 SDK:

$pubnub->history()
->channel(String)
->reverse(bool)
->includeTimetoken(bool)
->start(integer)
->end(integer)
->count(integer)
->sync();
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel to return history messages from.
reverse
Type: Boolean
Default:
false
Traverse from oldest to newest when set to true.
includeTimetoken
Type: Boolean
Default:
false
Whether to include message timetokens in the response.
start
Type: Integer
Default:
n/a
Timetoken delimiting the start (exclusive) of the time slice.
end
Type: Integer
Default:
n/a
Timetoken delimiting the end (inclusive) of the time slice.
count
Type: Integer
Default:
n/a
Number of historical messages to return.
tip
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.

Sample code

Retrieve the last 5 messages on a channel:


Response

The history() operation returns a PNHistoryResult which contains the following operations:

MethodDescription
getMessages()
Type: Array
array of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details.
getStartTimetoken()
Type: Integer
Start timetoken.
getEndTimetoken()
Type: Integer
End timetoken.

PNHistoryItemResult

MethodDescription
getTimetoken()
Type: Integer
Timetoken of the message.
getEntry()
Type: 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();
Response
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
)
show all 31 lines

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();
Response
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
show all 36 lines

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();
Response
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
show all 35 lines

Include timetoken in history response

$pubnub->history()
->channel("my_channel")
->count(100)
->includeTimetoken(true)
->sync();

Delete messages from history

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.

Remove messages from the history of a specific channel.

Required setting

Enable Delete-From-History for your key in the Admin Portal and initialize the SDK with a secret key.

Method(s)

To Delete Messages from History you can use the following method(s) in the PHP SDK.

$pubnub->deleteMessages()
->channel(String)
->start(integer)
->end(integer)
->sync()
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel to delete messages from.
start
Type: Integer
Default:
n/a
Timetoken delimiting the start (inclusive) of the time slice.
end
Type: Integer
Default:
n/a
Timetoken delimiting the end (exclusive) of the time slice.

Sample code


Other examples

Delete specific message from history

To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. For example, if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.

$pubnub->deleteMessages()
->channel("ch")
->start(15526611838554309)
->end(15526611838554310)
->sync();

Message counts

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.

Return the number of messages published since the given time. The count is the number of messages with a timetoken greater than or equal to the value in channelsTimetoken.

Unlimited message retention

Only messages from the last 30 days are counted.

Method(s)

You can use the following method(s) in the PHP SDK:

$pubnub->messageCounts()
->channels(array)
->channelsTimetoken(array)
* required
ParameterDescription
channels *
Type: Array
Default:
n/a
Channels to fetch the message count.
channelsTimetoken *
Type: Array
Default:
n/a
Array in the same order as channels; a single timetoken applies to all channels; otherwise, lengths must match or the function returns a PNStatus error.

Sample code


Returns

The operation returns a PNMessageCountsResult which contains the following operations

MethodDescription
getChannels()
Type: 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.

Other examples

Retrieve count of messages using different timetokens for each channel

$response = $pubnub->messageCounts()
->channels(["mychannel", "another_channel"])
->channelsTimetoken(["15513576173381797","15513574291261651"])
->sync();

print_r($response->getChannels());
Last updated on