Message Persistence API for PubNub Angular2 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 actions
- File Sharing (using File Sharing API)
History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
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 Angular2 SDK:
history( {String channel, Boolean reverse, Number count, Boolean stringifiedTimeToken, Boolean includeMeta, String start, String end}, Function callback )
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
Operation Arguments | Hash | Yes | A hash of arguments. | |
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.If both start and end arguments are provided, reverse is ignored and messages are returned starting with the newest message . |
count | Number | Optional | 100 | Specifies the number of historical messages to return. Default/Maximum is 100 . |
stringifiedTimeToken | Boolean | Optional | false | If stringifiedTimeToken is specified as true , the SDK will return timetoken values as a strings instead of integers. Usage of setting is encouraged in javascript environments which perform round-up/down on large integers. |
includeMeta | Boolean | Optional | Whether message meta information should be fetched or not. | |
start | String | Optional | Timetoken delimiting the start of time slice (exclusive) to pull messages from. | |
end | String | Optional | Timetoken delimiting the end of time slice (inclusive) to pull messages from. | |
callback | Function | Optional | Executes on a successful/unsuccessful history . |
tip
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:
pubnub.history(
{
channel: 'my_channel',
count: 100, // 100 is the default
stringifiedTimeToken: true // false is the default
},
function (status, response)
{
console.log(response);
}
);
Response
// Example of Status
{
error: false,
operation: "PNHistoryOperation",
statusCode: 200
}
// Example of Response
{
endTimeToken: "14867650866860159",
messages: [
{
timetoken: "14867650866860159",
entry: "[User 636] hello World"
},
show all 21 linesOther Examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse
// get last 3 messages published to my_channel
pubnub.history(
{
channel: ['my_channel'],
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
count: 3,
stringifiedTimeToken: true // false is the default
},
function (status, response) {
// handle status, response
}
);
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)
// get messages starting at timetoken order oldest first
pubnub.history(
{
channel: 'my_channel',
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
stringifiedTimeToken: true, // false is the default
start: '13406746780720711'
},
function (status, response) {
// handle status, response
}
);
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)
// return 100 messages ending on timetoken
pubnub.history(
{
channel: 'my_channel',
stringifiedTimeToken: true,
end: '13406746780720711'
},
function (status, response) {
// handle status, response
}
)
History Paging Example
Usage!
You can call the method by passing 0 or a valid timetoken as the argument.
getAllMessages = function(timetoken) {
pubnub.history(
{
channel: 'history_test',
stringifiedTimeToken: true, // false is the default
start: timetoken
},
function (status, response) {
var msgs = response.messages;
var start = response.startTimeToken;
var end = response.endTimeToken;
// if msgs were retrieved, do something useful with them
if (msgs !== "undefined" && msgs.length > 0) {
console.log(msgs.length);
console.log("start : " + start);
show all 29 linesBasic usage using Promises
pubnub.history({
channel: 'my_channel',
reverse: false, // false is the default
count: 100, // 100 is the default
stringifiedTimeToken: true // false is the default
}).then((response) => {
console.log(response);
}).catch((error) => {
console.log(error);
});
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse using Promises
// get last 3 messages published to my_channel
pubnub.history({
channel: ['my_channel'],
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
count: 3,
stringifiedTimeToken: true // false is the default
}).then((response) => {
console.log(response);
}).catch((error) => {
console.log(error);
});
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) using Promises
pubnub.history({
channel: 'my_channel',
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
stringifiedTimeToken: true, // false is the default
start: '13406746780720711' // start timetoken to fetch
}).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
});
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) using Promises
pubnub.history({
channel: 'my_channel',
stringifiedTimeToken: true, // false is the default
end: '13406746780720711' // start timetoken to fetch
}).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
});
Paging History Responses using Promises
function getAllMessages(timetoken) {
pubnub.history(
{
channel: 'history_test',
stringifiedTimeToken: true, // false is the default
start: timetoken // start timetoken to fetch
}).then((response) => {
var msgs = response.messages;
var start = response.startTimeToken;
var end = response.endTimeToken;
// if msgs were retrieved, do something useful with them
if (msgs !== undefined && msgs.length > 0) {
console.log(msgs.length);
console.log("start : " + start);
show all 30 lines