Message Persistence API for PubNub Android 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.
Fetch History
Requires Message Persistence add-onRequires 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 from one or multiple channels. The includeMessageActions
flag also allows you to fetch message actions 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 storage.
Method(s)
To run Fetch History
, you can use the following method(s) in the Android V4 SDK:
this.pubnub.fetchMessages().channels(List<String>).maximumPerChannel(Integer).start(Long).end(Long).includeMessageActions(Boolean).includeMeta(Boolean).includeMessageType(Boolean).includeUUID(Boolean)
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
channels | List | Yes | Specifies channels to return history messages from. Maximum of 500 channels are allowed. | |
start | Long | Optional | Timetoken delimiting the start of time slice (exclusive) to pull messages from. | |
end | Long | Optional | Timetoken delimiting the end of time slice (inclusive) to pull messages from. | |
maximumPerChannel | Integer | Optional | 100 or 25 | Specifies the number of historical messages to return per channel. Maximum value is 100 for a single channel, or 25 for multiple channels. If includeMessageActions is true , then 25 is the default (and maximum) value. |
includeMeta | Boolean | Optional | false | Whether to include message metadata within response or not. |
includeMessageActions | Boolean | Optional | false | The flag denoting to retrieve history messages with message actions. If true , the method is limited to one channel only. |
includeMessageType | Boolean | Optional | true | The flag denoting to retrieve history messages with message type. |
includeUUID | Boolean | Optional | true | The flag denoting to retrieve history messages with UUID of a publisher. |
async | PNCallback | Yes | PNCallback of type PNFetchMessagesResult . |
Truncated response
If you fetch messages with messages actions, the number of messages in the response may be truncated when internal limits are hit. If the response is truncated, a more
property will be returned with additional parameters. Send iterative calls to history adjusting the parameters to fetch more messages.
Basic Usage
Retrieve the last message on a channel:
pubnub.fetchMessages()
.channels(Arrays.asList("my_channel"))
.maximumPerChannel(25)
.includeMessageActions(true)
.includeMeta(true)
.includeMessageType(true)
.includeUUID(true)
.async(new PNCallback<PNFetchMessagesResult>() {
@Override
public void onResponse(PNFetchMessagesResult result, PNStatus status) {
if (!status.isError()) {
Map<String, List<PNFetchMessageItem>> channels = result.getChannels();
for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
System.out.println(messageItem.getMessage());
System.out.println(messageItem.getMeta());
System.out.println(messageItem.getTimetoken());
System.out.println(messageItem.getMessagetType());
System.out.println(messageItem.getUuid());
HashMap<String, HashMap<String, List<PNFetchMessageItem.Action>>> actions =
messageItem.getActions();
for (String type : actions.keySet()) {
System.out.println("Action type: " + type);
for (String value : actions.get(type).keySet()) {
System.out.println("Action value: " + value);
for (PNFetchMessageItem.Action action : actions.get(type).get(value)) {
System.out.println("Action timetoken: " + action.getActionTimetoken());
System.out.println("Action publisher: " + action.getUuid());
}
}
}
}
} else {
status.getErrorData().getThrowable().printStackTrace();
}
}
});
Returns
The fetchMessages()
operation returns a list of PNFetchMessagesResult
objects, each containing the following operations:
Method | Type | Description |
---|---|---|
getMessage() | JsonElement | Message content. |
getMeta() | JsonElement | Message metadata if any, and if requested via includeMeta(true) . |
getTimetoken() | Long | Publish timetoken. |
getActionTimetoken() | Long | Timestamp when the message action was created. |
getActions() | HashMap | Actions data of the message, if any, and if requested via includeMessageActions(true) . |
getMessageType() | Integer | Message type 0 - message, 1 - signal, 2 - object, 3 - message action, 4 - files |
getUuid() | String | UUID of the publisher |
Other Examples
-
package com.we.pubnubtest.utils; import com.pubnub.api.models.consumer.history.PNFetchMessagesResult; public abstract class CallbackSkeleton { public CallbackSkeleton() { } public abstract void handleResponse(PNFetchMessagesResult result); public abstract void finish(); }
package com.we.pubnubtest.utils; import com.pubnub.api.PubNub; import com.pubnub.api.callbacks.PNCallback; import com.pubnub.api.models.consumer.PNStatus; import com.pubnub.api.models.consumer.history.PNFetchMessagesResult; import com.pubnub.api.models.consumer.pubsub.PNMessageResult; import java.util.Arrays; import java.util.List; import java.util.concurrent.CountDownLatch; public class PubnubRecursiveHistoryFetcher { private PubNub pubNub; private String channels; public PubnubRecursiveHistoryFetcher(PubNub pubNub, String channels) { this.pubNub = pubNub; this.channels = channels; } public void getAllMessages(CallbackSkeleton callback) { getAllMessages(null, callback); } public void getAllMessages(Long startTimestamp, final CallbackSkeleton callback) { final CountDownLatch latch = new CountDownLatch(1); final Long timeToken; if (startTimestamp == null) { startTimestamp = -1L; } timeToken = startTimestamp; pubNub.fetchMessages() .channels(Arrays.asList(channels)) // where to fetch history from .start(startTimestamp) // first timestamp .maximumPerChannel(25) .async(new PNCallback<PNFetchMessagesResult>() { @Override public void onResponse(PNFetchMessagesResult result, PNStatus status) { // handle resposne here if (!status.isError() && result.getChannels().size()!=0) { callback.handleResponse(result); Long timetoken = timeToken; for (List<PNMessageResult> itens: result.getChannels().values()) { for (PNMessageResult item: itens) { if (item.getTimetoken()<timetoken) { timetoken = item.getTimetoken(); } } } getAllMessages(timetoken, callback); } else { callback.finish(); } } }); } }
Fetch Messages From Multiple Channels:
pubNub.fetchMessages() .channels(Arrays.asList("ch1", "ch2", "ch3")) .async(new PNCallback<PNFetchMessagesResult>() { @Override public void onResponse(@Nullable final PNFetchMessagesResult result, @NotNull final PNStatus status) { if (!status.isError()) { final Map<String, List<PNFetchMessageItem>> channelToMessageItemsMap = result.getChannels(); final Set<String> channels = channelToMessageItemsMap.keySet(); for (final String channel : channels) { List<PNFetchMessageItem> pnFetchMessageItems = channelToMessageItemsMap.get(channel); for (final PNFetchMessageItem fetchMessageItem: pnFetchMessageItems) { System.out.println(fetchMessageItem.getMessage()); System.out.println(fetchMessageItem.getMeta()); System.out.println(fetchMessageItem.getTimetoken()); } } } else { System.err.println("Handling error"); } } });
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 Android V4 SDK.
this.pubnub.deleteMessages().channels(Array).start(Long).end(Long)
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
channels | Array | Yes | Specifies channels to delete messages from. | |
start | Long | Optional | Timetoken delimiting the start of time slice (inclusive) to delete messages from. | |
end | Long | Optional | Timetoken delimiting the end of time slice (exclusive) to delete messages from. | |
async | PNCallback | Yes | PNCallback of type PNDeleteMessagesResult . |
Basic Usage
pubnub.deleteMessages()
.channels(Arrays.asList("channel_1", "channel_2"))
.start(1460693607379L)
.end(1460893617271L)
.async(new PNCallback<PNDeleteMessagesResult>() {
@Override
public void onResponse(PNDeleteMessagesResult result, PNStatus status) {
// The deleteMessages() method does not return actionable data, be sure to check the status
// object on the outcome of the operation by checking the status.isError().
}
});
Other Examples
Delete specific message from history
To delete a specific message, pass thepublish 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() .channels(Arrays.asList("channel_1")) .start(15526611838554310L) .end(15526611838554309L) .async(new PNCallback<PNDeleteMessagesResult>() { @Override public void onResponse(PNDeleteMessagesResult result, PNStatus status) { // The deleteMessages() method does not return actionable data, be sure to check the status // object on the outcome of the operation by checking the status.isError(). } });
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 Android V4 SDK:
this.pubnub.messageCounts().channels(Array).channelsTimetoken(Array)
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
channels | Array | Yes | The channels to fetch the message count | |
channelsTimetoken | Array | Yes | List of timetokens , in order of the channels list. Specify a single timetoken to apply it to all channels. Otherwise, the list of timetokens must be the same length as the list of channels, or the function returns a PNStatus with an error flag. | |
async | PNCallback | Yes | PNCallback of type PNMessageCountResult . |
Basic Usage
Long lastHourTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(1)) * 10000L;
pubnub.messageCounts()
.channels(Arrays.asList("news"))
.channelsTimetoken(Arrays.asList(lastHourTimetoken))
.async(new PNCallback<PNMessageCountResult>() {
@Override
public void onResponse(PNMessageCountResult result, PNStatus status) {
if (!status.isError()) {
for (Map.Entry<String, Long> messageCountEntry : result.getChannels().entrySet()) {
messageCountEntry.getKey(); // the channel name
messageCountEntry.getValue(); // number of messages for that channel
}
} else {
// Handle error accordingly.
status.getErrorData().getThrowable().printStackTrace();
}
}
});
Returns
The operation returns a PNMessageCountResult
which contains the following operations
Method | Type | Description |
---|---|---|
getChannels() | Map<String, Long> | A map with values of Long for each channel. 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
Long lastHourTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(1)) * 10000L; Long lastDayTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.DAYS.toMillis(1)) * 10000L; pubnub.messageCounts() .channels(Arrays.asList("news", "info")) .channelsTimetoken(Arrays.asList(lastHourTimetoken, lastDayTimetoken)) .async(new PNCallback<PNMessageCountResult>() { @Override public void onResponse(PNMessageCountResult result, PNStatus status) { if (!status.isError()) { for (Map.Entry<String, Long> messageCountEntry : result.getChannels().entrySet()) { messageCountEntry.getKey(); // the channel name messageCountEntry.getValue(); // number of messages for that channel } } else { // Handle error accordingly. status.getErrorData().getThrowable().printStackTrace(); } } });
History (deprecated)
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-
Note
This method is deprecated. Use fetch history instead.
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 Android V4 SDK:
this.pubnub.history().channel(String).reverse(Boolean).includeTimetoken(Boolean).includeMeta(Boolean).start(Long).end(Long).count(Integer);
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
channel | String | Yes | Specifies channel to return history messages from. | |
reverse | Boolean | Optional | false | Setting to true traverses 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. |
includeMeta | Boolean | Optional | false | Whether to include message metadata within response or not. |
start | Long | Optional | Timetoken delimiting the start of time slice (exclusive) to pull messages from. | |
end | Long | Optional | Timetoken delimiting the end of time slice (inclusive) to pull messages from. | |
count | Int | Optional | 100 | Specifies the number of historical messages to return. |
async | PNCallback | Yes | PNCallback of type PNHistoryResult . |
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:
pubnub.history()
.channel("history_channel") // where to fetch history from
.count(100) // how many items to fetch
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {
}
});
Returns
The history()
operation returns a PNHistoryResult
which contains the following operations:
Method | Type | Description |
---|---|---|
getMessages() | List | List of messages of type PNHistoryItemResult . See PNHistoryItemResult for more details. |
getStartTimetoken() | Long | Start timetoken . |
getEndTimetoken() | Long | End timetoken . |
Method | Type | Description |
---|---|---|
getTimetoken() | Long | Timetoken of the message. |
getEntry() | JsonElement | Message. |
Other Examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse:
pubnub.history() .channel("my_channel") // where to fetch history from .count(3) // how many items to fetch .reverse(true) // should go in reverse? .async(new PNCallback<PNHistoryResult>() { @Override public void onResponse(PNHistoryResult result, PNStatus status) { } });
if (!status.isError()) { for (PNHistoryItemResult pnHistoryItemResult: result.getMessages()) { pnHistoryItemResult.getEntry(); // custom JSON structure for message } }
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") // where to fetch history from .start(13847168620721752L) // first timestamp .reverse(true) // should go in reverse? .async(new PNCallback<PNHistoryResult>() { @Override public void onResponse(PNHistoryResult result, PNStatus status) { } });
if (!status.isError()) { for (PNHistoryItemResult pnHistoryItemResult: result.getMessages()) { pnHistoryItemResult.getEntry(); // custom JSON structure for message } }
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") // where to fetch history from .count(100) // how many items to fetch .start(-1) // first timestamp .end(13847168819178600L) // last timestamp .reverse(true) // should go in reverse? .async(new PNCallback<PNHistoryResult>() { @Override public void onResponse(PNHistoryResult result, PNStatus status) { } });
[ ["Pub3","Pub4","Pub5"], 13406746780720711, 13406746845892666 ]
-
Usage!
You can call the method by passing 0 or a valid timetoken as the argument.
package com.pubnub.api; import com.pubnub.api.PNConfiguration; import com.pubnub.api.PubNub; import com.pubnub.api.callbacks.PNCallback; import com.pubnub.api.models.consumer.PNStatus; import com.pubnub.api.models.consumer.history.PNHistoryItemResult; import com.pubnub.api.models.consumer.history.PNHistoryResult; public class PubNubRecursiveHistoryFetcher { private PubNub pubnub; private static abstract class CallbackSkeleton { public abstract void handleResponse(PNHistoryResult result); } private PubNubRecursiveHistoryFetcher() { // NOTICE: for demo/demo pub/sub keys Message Persistence is disabled, // so use your pub/sub keys instead PNConfiguration pnConfiguration = new PNConfiguration(); pnConfiguration.setSubscribeKey("demo"); pubnub = new PubNub(pnConfiguration); } public static void main(String[] args) { PubNubRecursiveHistoryFetcher fetcher = new PubNubRecursiveHistoryFetcher(); fetcher.getAllMessages("my_channel", null, 10, new CallbackSkeleton() { @Override public void handleResponse(PNHistoryResult result) { for (PNHistoryItemResult message : result.getMessages()) { System.out.println(message.getEntry()); } } }); } /** * Fetches channel history in a recursive manner, in chunks of specified size, starting from the most recent, * with every subset (with predefined size) sorted by the timestamp the messages were published. * * @param channel The channel where to fetch history from * @param start The timetoken which the fetching starts from * @param count Chunk size * @param callback Callback which fires when a chunk is fetched */ private void getAllMessages(final String channel, Long start, final int count, final CallbackSkeleton callback) { pubnub.history() .channel(channel) .count(count) .start(start) .includeTimetoken(true) .async(new PNCallback<PNHistoryResult>() { @Override public void onResponse(PNHistoryResult result, PNStatus status) { if (!status.isError() && !result.getMessages().isEmpty()) { callback.handleResponse(result); getAllMessages(channel, result.getMessages().get(0).getTimetoken(), count, callback); } } }); } }
Include timetoken in history response:
pubnub.history() .channel("history_channel") // where to fetch history from .count(100) // how many items to fetch .includeTimetoken(true) // include timetoken with each entry .async(new PNCallback<PNHistoryResult>() { @Override public void onResponse(PNHistoryResult result, PNStatus status) { } });