Message Persistence API: deprecated history() method and actions data property for JavaScript SDK
This page documents the deprecated pubnub.history() method and the deprecated data property of actions in fetchMessages() responses in the PubNub JavaScript SDK. Message Persistence is a current API, and only these two are deprecated. They remain for apps that already use them. New apps call fetchMessages() and read actions.
History (deprecated)
Deprecated
history() is deprecated in the JavaScript SDK. Use fetchMessages() instead.
Requires Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
history() fetches the messages of one channel. You can control how it returns messages and in what order:
- By default, the search starts at the newest end of the timeline, which is
reverseset tofalse. - To search from the oldest end of the timeline, set
reversetotrue. - To page through results, provide
startorend. - To retrieve a slice of the timeline, provide both
startandend. - To limit the number of messages, use
count.
Method(s)
1pubnub.history({
2 channel: string,
3 reverse?: boolean,
4 count?: number,
5 stringifiedTimeToken?: boolean,
6 includeMeta?: boolean,
7 start?: string,
8 end?: string
9}): Promise<GetHistoryResponse>;
channelrequired#- ID of the channel to return history messages from.
reverse#- Whether to traverse the timeline from the oldest message to the newest. If you provide both
startandend,history()ignoresreverseand returns the messages starting with the newest one. count#- Number of messages to return. The maximum is
100, andhistory()lowers a larger value to100. stringifiedTimeToken#- Whether to return timetokens as strings.
includeMeta#- Whether to include the
metaobject, if the publisher set one at publish time, in each message of the response. start#- Timetoken that sets the newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. With
end,startmust be a higher timetoken thanend. end#- Timetoken that sets the older boundary of the query (inclusive). Retrieval stops at this timetoken. With
start,endmust be a lower timetoken thanstart.
history() always returns the messages sorted from the oldest to the newest, whatever reverse is. The reverse setting matters when more than 100 messages, or more than count if you set it, fall in the time interval. In that case, reverse decides which end of the interval the retrieval starts from.
Sample code
Retrieve the last 100 messages on a channel:
1
Returns
history() resolves with an object that has these properties. With a callback, status.operation is PNHistoryOperation.
In each item of messages, entry holds the decrypted message payload and timetoken holds the message timetoken. The meta property is present only when the publisher set metadata. The error property is present only when the SDK fails to decrypt the message.
1{
2 endTimeToken: "14867650866860159",
3 messages: [
4 {
5 timetoken: "14867650866860159",
6 entry: "[User 636] hello World"
7 },
8 {...},
9 {...},
10 {...}
11 ],
12 startTimeToken: "14867650866860159"
13}
Other examples
Retrieve the three oldest messages
Set reverse to true to traverse the timeline from the oldest message first.
1
Retrieve messages newer than a timetoken
Page from the oldest message to the newest, starting at a single point in time (exclusive).
1
Retrieve messages until a timetoken
Page from the newest message to the oldest, until a specific point in time (inclusive).
1
Page through all messages
Call the function with no argument or with a valid timetoken.
1
Fetch messages with metadata
1
Call history with promises
1
Deprecated data property of actions
Deprecated
The data property of actions is deprecated. Read actions instead.
fetchMessages() returns message actions on each message in actions, grouped by action type and then by action value. The response has them only with includeMessageActions and when the message has actions. The deprecated data property holds the same value as actions.
data#- Holds the same value as
actions.- Replacement
actions
See Fetch history for the full response.