Message Persistence API for PHP SDK
Message Persistence provides real-time access to the history of 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 locations. Stored messages can be encrypted with AES-256 message encryption, ensuring that they are not readable while stored on PubNub's network. For more information, refer to Message Persistence.
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 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message reactions
- File Sharing (using File Sharing API)
Fetch History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
This function fetches historical messages from one or multiple channels. The includeMessageActions
flag also allows you to fetch message reactions along with the messages.
It's possible to control how messages are returned and in what order.
- if you specify only the
start
parameter (withoutend
), you will receive messages that are older than thestart
timetoken - if you specify only the
end
parameter (withoutstart
), you will receive messages from thatend
timetoken and newer - if you specify values for both
start
andend
parameters, you will retrieve messages between those timetokens (inclusive of theend
value)
You will receive a maximum of 100 messages for a single channel or 25 messages for multiple channels (up to 500). If more messages meet the timetoken criteria, make iterative calls while adjusting the start
timetoken to fetch the entire list of messages from Message Persistence.
Method(s)
To run Fetch History
, you can 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)
Parameter | Description |
---|---|
channels *Type: string or Array<string> Default: n/a | Specifies the channels for which to return history. Maximum of 500 channels are allowed. |
maximumPerChannel Type: Int Default: 25 or 100 | Specifies the number of historical messages to return. If includeMessageActions is True , method is limited to single channel and 25 is the default (and maximum) value; otherwise, default and maximum is 100 for a single channel, or 25 for multiple channels. |
start Type: string Default: n/a | Timetoken delimiting the exclusive start of the time slice from which to pull messages. |
end Type: string Default: n/a | Timetoken delimiting the inclusive end of the time slice from which to pull messages. |
includeMessageActions Type: Boolean Default: False | Set to True to retrieve history messages with their associated message reactions. If you include message reactions, the Messages()` method is limited to one channel only. |
includeMeta Type: Boolean Default: False | Whether to include message metadata within response or not. |
includeMessageType Type: Boolean Default: n/a | Indicates whether to retrieve messages with PubNub message type. For more information, refer to Retrieving Messages. |
includeCustomMessageType Type: Boolean Default: n/a | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
includeUuid Type: Boolean Default: n/a | Whether to include user ID of the sender. |
Basic Usage
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.
<?php
// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';
use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubServerException;
use PubNub\Exceptions\PubNubException;
// Create configuration with demo keys
$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey("demo");
$pnConfig->setSubscribeKey("demo");
$pnConfig->setUserId("fetch-messages-demo-user");
show all 110 linesReturns
The fetchMessages()
operation returns an PNFetchMessagesResult
which contains the following fields:
PNFetchMessagesResult
Method | Description |
---|---|
channels Type: Array | Array of PNFetchMessageItem |
startTimetoken Type: Int | Start timetoken. |
endTimetoken Type: Int | End timetoken. |
PNFetchMessageItem
Method | Description |
---|---|
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 reactions, grouped by action type and value |
History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
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 SDK:
$pubnub->history()
->channel(String)
->reverse(bool)
->includeTimetoken(bool)
->start(integer)
->end(integer)
->count(integer)
->sync();
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Specifies channel to return history messages from. |
reverse Type: Boolean Default: false | Setting to true will traverse the time line in reverse starting with the oldest message first. |
includeTimetoken Type: Boolean Default: false | Whether event dates timetokens should be included in response or not. |
start Type: Integer Default: n/a | Timetoken delimiting the start of time slice (exclusive) to pull messages from. |
end Type: Integer Default: n/a | Timetoken delimiting the end of time slice (inclusive) to pull messages from. |
count Type: Integer Default: n/a | Specifies the number of historical messages to return. |
tip
reverse
parameterMessages 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.