Message Reactions API for 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.

Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message reaction events on that channel. They can also fetch past message reactions from Message Persistence independently or when they fetch original messages.

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
}
}
Terminology

All message reactions in code are collectively called message reactions in the docs and the term applies both to reacting to a message (through emojis) and acting on a message (by deleting it).

Add Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action on a published message. Returns the added action in the response.

Method(s)

To Add a Message Reaction you can use the following method(s) in the Kotlin SDK:

pubnub.addMessageAction(
channel: String,
messageAction: PNMessageAction
).async { result -> }
* required
ParameterDescription
channel *
Type: String
Specifies channel name to publish message reactions to.
messageAction *
Type: PNMessageAction
The message reaction object containing the message reaction's type, value and the publish timetoken of the original message. See PNMessageAction for more details.

Basic Usage

import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.models.consumer.message_actions.PNMessageAction

fun main() {
println("PubNub addMessageAction() Example")
println("================================")

// 1. Configure PubNub
val userId = UserId("message-action-demo-user")
val config = PNConfiguration.builder(userId, "demo").apply {
publishKey = "demo"
logVerbosity = PNLogVerbosity.BODY // Enable debug logging for visibility
show all 131 lines

Returns

The addMessageAction() operation returns a PNAddMessageActionResult which contains the following operations:

MethodTypeDescription
type
String
Message reaction's type.
value
String
Message reaction's value.
uuid
String
Publisher of the message reaction.
actionTimetoken
String
Timestamp when the message reaction was created.
messageTimetoken
Long
Timestamp when the actual message was created the message reaction belongs to.

PNMessageAction

MethodTypeDescription
type
String
Message reaction's type.
value
String
Message reaction's value.
messageTimetoken
Long
Timestamp when the actual message was created the message reaction belongs to.

Remove Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a previously added action on a published message. Returns an empty response.

Method(s)

To Remove a Message Reaction you can use the following method(s) in the Kotlin SDK:

pubnub.removeMessageAction(
channel: String,
messageTimetoken: Long,
actionTimetoken: Long
).async { result -> }
* required
ParameterDescription
channel *
Type: String
Specifies channel name to remove message reactions from.
messageTimetoken *
Type: Long
Publish timetoken of the original message.
actionTimetoken *
Type: Long
Publish timetoken of the message reaction to be removed.

Basic Usage

pubnub.removeMessageAction(
channel = "my_channel",
messageTimetoken = 15701761818730000L,
actionTimetoken = 15701775691010000L
).async { result: Result<PNRemoveMessageActionResult> ->
result.onSuccess { res: PNRemoveMessageActionResult ->
// result has no actionable data
// it's enough to check if the status itself is not an error
}.onFailure { e: PubNubException ->
// do something with the exception
e.message
e.statusCode
e.pubnubError
}
}

Returns

The removeMessageAction() operation returns a no actionable data.

Get Message Reactions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Get a list of message reactions in a channel. Returns a list of actions sorted by the action's timetoken in ascending order.

Method(s)

To Get Message Reactions you can use the following method(s) in the Kotlin SDK:

pubnub.getMessageActions(
channel: String,
page: PNBoundedPage
)
* required
ParameterDescription
channel *
Type: String
The channel name.
page
Type: PNBoundedPage
The paging object used for pagination. Set limit to specify the number of actions to return in response.
Default/Maximum is 100.
start and end denotes the range boundaries. Return values will be less than start and greater or equal to end.

Basic Usage

pubnub.getMessageActions(
channel = "my_channel"
).async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Returns

The getMessageActions() operation returns a list of PNGetMessageActionsResult? objects, each containing the following operations:

MethodTypeDescription
type
String
Message reaction's type.
value
String
Message reaction's value.
uuid
String
Publisher of the message reaction.
actionTimetoken
String
Timestamp when the message reaction was created.
messageTimetoken
Long
Timestamp when the actual message was created the message reaction belongs to.
page
PNBoundedPage
If exists indicates that more data is available. It can be passed directly to another call of getMessageActions to retrieve this remaining data.

Other Examples

Fetch Messages with paging

fun main() {
getMessageActionsWithPaging(
channel = "my_channel",
start = System.currentTimeMillis() * 10_000L
) { actions ->
actions.forEach {
println(it.type)
println(it.value)
println(it.uuid)
println(it.messageTimetoken)
println(it.actionTimetoken)
}
}
}

show all 46 lines
Last updated on