Message Persistence API for PubNub Java SDK

Breaking changes in v9.0.0

PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0) of the Java SDK.

For more details about what has changed, refer to Java/Kotlin SDK migration guide.

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. Read the support page on enabling add-on features on your keys.

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 (without end), you will receive messages that are older than the start timetoken
  • if you specify only the end parameter (without start), you will receive messages from that end timetoken and newer
  • if you specify values for both start and end parameters, you will retrieve messages between those timetokens (inclusive of the end 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 Java SDK:

this.pubnub.fetchMessages()
.channels(List<String>)
.maximumPerChannel(Integer)
.start(Long)
.end(Long)
.includeMessageActions(Boolean)
.includeMeta(Boolean)
.includeMessageType(Boolean)
.includeCustomMessageType(Boolean)
.includeUUID(Boolean)
ParameterTypeRequiredDefaultDescription
channelsList<String>YesSpecifies channels to return history messages from. Maximum of 500 channels are allowed.
maximumPerChannelIntegerOptional100 or 25Specifies 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.
startLongOptionalTimetoken delimiting the start of time slice (exclusive) to pull messages from.
endLongOptionalTimetoken delimiting the end of time slice (inclusive) to pull messages from.
includeMessageActionsBooleanOptionalfalseThe flag denoting to retrieve history messages with message reactions. If true, the method is limited to one channel and 25 messages only.
includeMetaBooleanOptionalfalseWhether to include message metadata within response or not.
includeMessageTypeBooleanOptionaltrueThe flag denoting to retrieve history messages with message type.
includeCustomMessageTypeBooleanNofalseIndicates whether to retrieve messages with the custom message type.

For more information, refer to Retrieving Messages.
includeUUIDBooleanOptionaltrueThe flag denoting to retrieve history messages with UUID of a publisher.
asyncConsumer<Result>YesConsumer of a Result 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)
.includeCustomMessageType(true)
.includeUUID(true)
.async(result -> {
result.onSuccess(res -> {
Map<String, List<PNFetchMessageItem>> channels = res.getChannels();
for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
System.out.println(messageItem.getMessage());
System.out.println(messageItem.getMeta());
System.out.println(messageItem.getTimetoken());
show all 34 lines

Returns

The fetchMessages() operation returns a list of PNFetchMessagesResult objects, each containing the following operations:

MethodTypeDescription
getMessage()JsonElementMessage content.
getMeta()JsonElementMessage metadata if any, and if requested via includeMeta(true).
getTimetoken()LongPublish timetoken.
getActionTimetoken()LongTimestamp when the message reaction was created.
getActions()HashMapActions data of the message, if any, and if requested via includeMessageActions(true).
getMessageType()IntegerMessage type 0 - message, 1 - signal, 2 - object, 3 - message reaction, 4 - files
getCustomMessageType()StringThe custom message type.
getUuid()StringUUID of the publisher

Other Examples

Paging History Responses

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.java.PubNub;
import com.pubnub.api.java.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;
show all 65 lines

Delete Messages from 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.

Removes the messages from the history of a specific channel.

Required setting

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 Java SDK.

this.pubnub.deleteMessages()
.channels(Array)
.start(Long)
.end(Long)
ParameterTypeRequiredDefaultDescription
channelsArrayYesSpecifies channels to delete messages from.
startLongOptionalTimetoken delimiting the start of time slice (inclusive) to delete messages from.
endLongOptionalTimetoken delimiting the end of time slice (exclusive) to delete messages from.
asyncConsumer<Result>YesConsumer of a Result of type PNDeleteMessagesResult.

Basic Usage

pubnub.deleteMessages()
.channels(Arrays.asList("channel_1", "channel_2"))
.start(1460693607379L)
.end(1460893617271L)
.async(result -> { /* check result */ });

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()
.channels(Arrays.asList("channel_1"))
.start(15526611838554309L)
.end(15526611838554310L)
.async(result -> { /* check result */ });

Message Counts

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.

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 channelsTimetokenparameter.

Unlimited message retention

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 Java SDK:

this.pubnub.messageCounts()
.channels(Array)
.channelsTimetoken(Array)
ParameterTypeRequiredDefaultDescription
channelsArrayYesThe channels to fetch the message count
channelsTimetokenArrayYesList 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.
asyncConsumer<Result>YesConsumer of a Result 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(result -> {
result.onSuccess(res -> {
for (Map.Entry<String, Long> messageCountEntry : res.getChannels().entrySet()) {
messageCountEntry.getKey(); // the channel name
messageCountEntry.getValue(); // number of messages for that channel
}
}).onFailure(exception -> {
exception.printStackTrace();
});
});

Returns

The operation returns a PNMessageCountResult which contains the following operations

MethodTypeDescription
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(result -> {
result.onSuccess(res -> {
for (Map.Entry<String, Long> messageCountEntry : res.getChannels().entrySet()) {
messageCountEntry.getKey(); // the channel name
messageCountEntry.getValue(); // number of messages for that channel
}
}).onFailure(exception -> {
exception.printStackTrace();
});
show all 16 lines

History (deprecated)

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.

Alternative method

This method is deprecated. Use fetch history instead.

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 Java SDK:

this.pubnub.history()
.channel(String)
.reverse(Boolean)
.includeTimetoken(Boolean)
.includeMeta(Boolean)
.start(Long)
.end(Long)
.count(Integer);
ParameterTypeRequiredDefaultDescription
channelStringYesSpecifies channelto return history messages from.
reverseBooleanOptionalfalseSetting to true traverses the time line in reverse, starting with the oldest message first.
includeTimetokenBooleanOptionalfalseWhether event dates timetokens should be included in response or not.
includeMetaBooleanOptionalfalseWhether to include message metadata within response or not.
startLongOptionalTimetoken delimiting the start of time slice (exclusive) to pull messages from.
endLongOptionalTimetoken delimiting the end of time slice (inclusive) to pull messages from.
countIntOptional100Specifies the number of historical messages to return.
asyncConsumer<Result>YesConsumer of a Result of type PNHistoryResult.
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.

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(result -> { /* check result */ });

Returns

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

MethodTypeDescription
getMessages()List<PNHistoryItemResult>List of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details.
getStartTimetoken()LongStart timetoken.
getEndTimetoken()LongEnd timetoken.

PNHistoryItemResult

MethodTypeDescription
getTimetoken()LongTimetoken of the message.
getEntry()JsonElementMessage.

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(result -> { /* check result */ });
Response
result.onSuccess(res -> {
for (PNHistoryItemResult pnHistoryItemResult: res.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(result -> { /* check result */ });
Response
result.onSuccess(res -> {
for (PNHistoryItemResult pnHistoryItemResult: res.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(result -> { /* check result */ });
Response
[
["Pub3","Pub4","Pub5"],
13406746780720711,
13406746845892666
]

History Paging Example

Usage

You can call the method by passing 0 or a valid timetoken as the argument.

package com.pubnub.api;

import com.pubnub.api.java.v2.PNConfiguration;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.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);
show all 62 lines

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(result -> { /* check result */ });
Last updated on