Message Persistence API for PubNub Kotlin SDK
Breaking changes in v9.0.0
PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java 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 Kotlin 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 actions
- File Sharing (using File Sharing API)
Request execution
Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously.
You must invoke the .sync()
or .async()
method on the Endpoint to execute the request, or the operation will not be performed.
val channel = pubnub.channel("channelName")
channel.publish("This SDK rules!").async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Batch 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 multiple channels. The includeMessageActions
or includeActions
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. For example, you can:
- Search for messages starting on the newest end of the timeline.
- Search for messages from the oldest end of the timeline.
- Page through results by providing a
start
ORend
timetoken. - Retrieve a slice of the time line by providing both a
start
ANDend
timetoken. - Retrieve a specific (maximum) number of messages using the
count
parameter.
Batch history returns up to 100 messages on a single channel, or 25 per channel on a maximum of 500 channels. Use the start
and end
timestamps to page through the next batch of messages.
Start & End parameter usage clarity
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.
Specify values for both start
and end
parameters to retrieve messages between those timetokens (inclusive of the end
value).
Keep in mind that you will still receive a maximum of 100 messages (or 25, for multiple channels) even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the start
timetoken are necessary to page through the full set of results if more messages meet the timetoken values.
Method(s)
To run fetchMessages()
, you can use the following method(s) in the Kotlin SDK:
pubnub.fetchMessages(
channels: List<String>,
page: PNBoundedPage,
includeMeta: Boolean,
includeMessageAction: Boolean,
includeMessageType: Boolean,
includeCustomMessageType: Boolean,
).async { result -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | List<String> | Yes | Specifies channels to return history messages from. | |
page | PNBoundedPage | Optional | The paging object used for pagination. Set limit to specify the number of historical messages to return per channel.If includeMessageActions is false , then 100 is the default (and maximum) value. Otherwise it's 25 .Set start to delimit the start of time slice (exclusive) to pull messages from.Set end to delimit the end of time slice (inclusive) to pull messages from. | |
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. |
includeCustomMessageType | Boolean | Optional | false | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
Basic Usage
Retrieve the last 25 messages on a channel:
pubnub.fetchMessages(
channels = listOf("ch1"),
page = PNBoundedPage(limit = 25),
includeMessageActions = true,
includeMeta = true
includeCustomMessageType = true
).async { result ->
result.onSuccess { value ->
value!!.channels.forEach { (channel, messages) ->
println("Channel: $channel")
messages.forEach { messageItem: PNFetchMessageItem ->
println(messageItem.message) // actual message payload
println(messageItem.timetoken) // included by default
messageItem.actions?.forEach { mapEntry: Map.Entry<String, Map<String, List<PNFetchMessageItem.Action>>> ->
println("Action type: ${mapEntry.key}")
show all 29 linesReturns
The fetchMessages()
operation returns a map of channels and List<PNFetchMessagesResult>
and PNBoundedPage
object.
Method | Type | Description |
---|---|---|
channels | HashMap<String, List<PNFetchMessageItem>> | Map of channels and their respective lists of PNFetchMessageItem . See PNFetchMessageItem for more details. |
page | PNBoundedPage | If exists indicates that more data is available. It can be passed directly to another call of fetchMessages to retrieve this remaining data. |
PNFetchMessageItem
Method | Type | Description |
---|---|---|
message | JsonElement | Message |
timetoken | Long | Timetoken of the message. Always returned by default. |
meta | JsonElement? | Metadata of the message. Is null if not requested, otherwise an empty string if requested but no associated metadata. |
actions | Map<String, HashMap<String, List<Action>>>? | The message actions associated with the message. Is null if not requested. The key of the map is the action type. The value is another map, which key is the actual value of the message action, and the key being a list of actions, ie. a list of UUIDs which have posted such a message action. See Action for more details. |
customMessageType | String | The custom message type. |
Action
Method | Type | Description |
---|---|---|
uuid | String | The UUID of the publisher. |
actionTimetoken | String | The publish timetoken of the action. |
Other Examples
Paging History Responses
fun main() {
getAllMessages(listOf("ch1", "ch2"), start = 0L) { result ->
result.channels.forEach { (channel, messages) ->
println("Channel $channel")
messages.forEach { item ->
println(item)
}
}
}
}
private fun getAllMessages(
channels: List<String>,
start: Long,
callback: (result: PNFetchMessagesResult) -> Unit
show all 38 linesDelete 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 deleteMessages()
you can use the following method(s) in the Kotlin SDK.
pubnub.deleteMessages(
channels:P List<String>,
start: Long,
end: Long
).async { result -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | List<String> | 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. |
Basic Usage
pubnub.deleteMessages(
channels = listOf("channel_1", "channel_2"),
start = 1460693607379L,
end = 1460893617271L
).async { _, status ->
// The deleteMessages() method does not return actionable data.
// Be sure to check the status on the outcome of the operation
// by checking the status.error
if (status.error) {
// handle error
status.exception?.printStackTrace()
}
}
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 = listOf("channel_1"),
start = 15526611838554309L,
end = 15526611838554310L
).async { _, status ->
// The deleteMessages() method does not return actionable data.
// Be sure to check the status on the outcome of the operation
// by checking the status.error
if (status.error) {
// handle error
status.exception?.printStackTrace()
}
}
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 channelsTimetoken
parameter.
Unlimited message retention
For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.
Method(s)
To run messageCounts()
, you can use the following method(s) in the Kotlin SDK:
pubnub.messageCounts(
channels: List<String>,
channelsTimetoken: List<Long>
).async { result -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | List<String> | Yes | The channels to fetch the message count. | |
channelsTimetoken | List<Long> | 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. |
Basic Usage
val lastHourTimetoken = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1)
pubnub.messageCounts(
channels = listOf("news"),
channelsTimetoken = listOf(lastHourTimetoken * 10_000L)
).async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Returns
Method | Type | Description |
---|---|---|
channels | 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
val lastHourTimetoken = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1)
val lastDayTimetoken = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)
pubnub.messageCounts(
channels = listOf("news", "info"),
channelsTimetoken = listOf(lastHourTimetoken, lastDayTimetoken).map { it * 10_000L }
).async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
value.channels.forEach { (channel, count) ->
println("$count new messages on $channel")
}
}
}
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.
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 Kotlin SDK:
pubnub.history(
channel: String,
reverse: Boolean,
includeTimetoken: Boolean,
includeMeta: Boolean,
start: Long,
end: Long,
count: Int
).async { result -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
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. |
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. Default/Maximum is 100 . |
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 = "history_channel", // where to fetch history from
count = 100 // how many items to fetch
).async { result -> }
Returns
The history()
operation returns a PNHistoryResult
which contains the following operations:
Method | Type | Description |
---|---|---|
messages | List<PNHistoryItemResult> | List of messages of type PNHistoryItemResult . See PNHistoryItemResult for more details. |
startTimetoken | Long | Start timetoken . |
endTimetoken | Long | End timetoken . |
PNHistoryItemResult
Method | Type | Description |
---|---|---|
timetoken | Long? | Timetoken of the message. Is null if not requested. |
entry | JsonElement | Message |
meta | JsonElement? | Metadata of the message. Is null if not requested, otherwise an empty string if requested but no associated metadata. |
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 -> }
Response:
if (!status.error) {
result!!.messages.forEach {
it.entry // 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 -> }
Response:
if (!status.error) {
result!!.messages.forEach {
it.entry // custom JSON structure for message
}
} else {
// handle error
status.exception?.printStackTrace()
}
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 -> }
Response:
if (!status.error) {
result!!.messages // ["Pub3","Pub4","Pub5"]
result.startTimetoken // 13406746780720711
result.endTimetoken // 13406746845892666
}
History Paging Example
fun main() {
getAllMessages("my_channel", start = 0L, count = 10) { result ->
result.messages.forEach {
println(it.entry)
}
}
}
/**
* 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
show all 39 linesInclude timetoken
in history response
pubnub.history(
channel = "history_channel", // where to fetch history from
count = 10, // how many items to fetch]
includeTimetoken = true // include timetoken with each entry
).async { result ->
result.onSuccess { value ->
value!!.messages.forEach {
it.entry // custom JSON structure for message
it.timetoken // requested message timetoken
}
}
}