Message Persistence API for 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 gives you real-time access to the history of messages published to PubNub. Each message is timestamped to the nearest 10 nanoseconds and stored across multiple availability zones in several geographic locations. You can encrypt stored messages with AES-256 so they are not readable on PubNub’s network. For details, see Message Persistence.
You control how long messages are stored through your account’s retention policy. Options include: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message reactions
- Files (using the File Sharing API)
Fetch history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Fetch historical messages from one or more channels. Use includeMessageActions
to include message actions.
You control how messages are returned and in what order:
- If you specify only the
start
parameter (withoutend
), you receive messages older than thestart
timetoken. - If you specify only the
end
parameter (withoutstart
), you receive messages from thatend
timetoken and newer. - If you specify both
start
andend
, you retrieve messages between those timetokens (inclusive of theend
value).
You can receive up to 100 messages for a single channel. For multiple channels (up to 500), you can receive up to 25 messages per channel. If more messages match the time range, make iterative calls and adjust the start
timetoken to page through the results.
Method(s)
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)
Parameter | Description |
---|---|
channels *Type: List <String> Default: n/a | Channels to fetch history messages from (up to 500). |
maximumPerChannel Type: Integer Default: 100 or 25 | Number of historical messages to return. Default and maximum are 100 (single), 25 (multi), and 25 with includeMessageActions . |
start Type: Long Default: n/a | Timetoken delimiting the start (exclusive) of the time slice. |
end Type: Long Default: n/a | Timetoken delimiting the end (inclusive) of the time slice. |
includeMessageActions Type: Boolean Default: false | Whether to retrieve history messages with message actions. If true , limited to one channel and 25 messages. Default is false . |
includeMeta Type: Boolean Default: false | Whether to include the meta object (if provided at publish time) in the response. |
includeMessageType Type: Boolean Default: true | Whether to pass true to include message type. Default is true . |
includeCustomMessageType Type: Boolean Default: false | Whether to retrieve messages with the custom message type. See Retrieving Messages. |
includeUUID Type: Boolean Default: true | Whether to receive the publisher uuid. Default is true . |
async *Type: Consumer<Result> Default: n/a | Consumer of a Result of type PNFetchMessagesResult . |
Truncated response
If you fetch messages with message actions, the response may be truncated when internal limits are reached. If truncated, a more
property is returned with additional parameters. Make iterative calls to history, adjusting the parameters to fetch more messages.
Sample code
Reference code
Retrieve the last message on a channel:
Returns
The fetchMessages()
operation returns a list of PNFetchMessagesResult
objects, each containing the following operations:
Method | Description |
---|---|
getMessage() Type: JsonElement | Message content. |
getMeta() Type: JsonElement | Message metadata if any, and if requested via includeMeta(true) . |
getTimetoken() Type: Long | Publish timetoken. |
getActionTimetoken() Type: Long | Timestamp when the message action was created. |
getActions() Type: HashMap | Actions data of the message, if any, and if requested via includeMessageActions(true) . |
getMessageType() Type: Integer | Message type 0 - message, 1 - signal, 2 - object, 3 - message action, 4 - files |
getCustomMessageType() Type: String | The custom message type. |
getUuid() Type: String | UUID of the publisher |
Other examples
Paging history responses
Delete messages from history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Remove messages from the history of a specific channel.
Required setting
Enable Delete-From-History
for your key in the Admin Portal and initialize the SDK with a secret key.
Method(s)
Use the following method(s) in the Java SDK:
this.pubnub.deleteMessages()
.channels(Array)
.start(Long)
.end(Long)
Parameter | Description |
---|---|
channels *Type: Array Default: n/a | Channels to delete messages from. |
start Type: Long Default: n/a | Timetoken delimiting the start (inclusive) of the time slice. |
end Type: Long Default: n/a | Timetoken delimiting the end (exclusive) of the time slice. |
async *Type: Consumer<Result> Default: n/a | Consumer of a Result of type PNDeleteMessagesResult . |
Sample code
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.
Message counts
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Return the number of messages published since the given time. The count is the number of messages with a timetoken greater than or equal to channelsTimetoken
.
Unlimited message retention
Only messages from the last 30 days are counted.
Method(s)
Use the following method(s) in the Java SDK:
this.pubnub.messageCounts()
.channels(Array)
.channelsTimetoken(Array)
Parameter | Description |
---|---|
channels *Type: Array Default: n/a | Channels to fetch the message count. |
channelsTimetoken *Type: Array Default: n/a | Array in the same order as channels; a single timetoken applies to all channels; otherwise, lengths must match exactly. |
async *Type: Consumer<Result> Default: n/a | Consumer of a Result of type PNMessageCountResult . |
Sample code
Returns
The operation returns a PNMessageCountResult
which contains the following operations
Method | Description |
---|---|
getChannels() Type: 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
History (deprecated)
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
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
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)
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);
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Channel to return history messages from. |
reverse Type: Boolean Default: false | Traverse from oldest to newest when set to true . |
includeTimetoken Type: Boolean Default: false | Whether to include message timetokens in the response. |
includeMeta Type: Boolean Default: false | Whether to include the meta object (if provided at publish time) in the response. |
start Type: Long Default: n/a | Timetoken delimiting the start (exclusive) of the time slice. |
end Type: Long Default: n/a | Timetoken delimiting the end (inclusive) of the time slice. |
count Type: Int Default: 100 | Number of historical messages to return. |
async *Type: Consumer<Result> Default: n/a | Consumer of a Result 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.
Sample code
Retrieve the last 100 messages on a channel:
Returns
The history()
operation returns a PNHistoryResult
which contains the following operations:
Method | Description |
---|---|
getMessages() Type: List <PNHistoryItemResult> | List of messages of type PNHistoryItemResult . See PNHistoryItemResult for more details. |
getStartTimetoken() Type: Long | Start timetoken . |
getEndTimetoken() Type: Long | End timetoken . |
PNHistoryItemResult
Method | Description |
---|---|
getTimetoken() Type: Long | Timetoken of the message. |
getEntry() Type: JsonElement | Message. |
Other examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse
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)
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)
History paging example
Usage
You can call the method by passing 0 or a valid timetoken as the argument.
Include timetoken in history