Manage message updates
Edit messages and receive events whenever someone edits them.
Requires Message Persistence
To manage messages in PubNub storage, you must enable Message Persistence for your app's keyset in the Admin Portal.
Edit messages
Change the content of the existing message to a new one using the editText()
method.
Method signature
This method takes the following parameters:
message.editText(newText: String): PNFuture<Message>
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
newText | String | Yes | n/a | New/updated text that you want to add in place of the existing message. |
Output
Type | Description |
---|---|
PNFuture<Message> | An updated message instance with an added edited action type. |
Basic usage
Correct the number of the support ticket you sent to 78398
.
message.editText("Support ticket number corrected to 78398").async { result ->
result.onSuccess {
// handle success
println("Ticket number successfully updated to 78398.")
}.onFailure {
// handle failure
println("Failed to update the ticket number.")
}
}
Get message updates
You can receive updates when specific messages and related message reactions are added, edited, or removed on other clients using the following methods:
streamUpdates()
checks message and message reaction-related updates on a singleMessage
object.streamUpdatesOn()
checks message and message reaction-related updates on a list ofMessage
objects.
Both methods accept a callback function as an argument. The Chat SDK invokes this callback whenever someone adds, edits or deletes a message, or adds or removes a message reaction to/from the specific message(s).
Underneath, these methods subscribe the current user to a channel and add a message reactions event listener to receive all messageAction
events of type added
or removed
. These methods also return the unsubscribe
function you can invoke to stop receiving messageAction
events and unsubscribe from the channel.
Method signature
These methods take the following parameters:
-
streamUpdates()
message.streamUpdates(callback: (message: T) -> Unit): AutoCloseable
-
streamUpdatesOn()
class Message {
companion object {
fun streamUpdatesOn(
messages: Collection<Message>,
callback: (messages: Collection<Message>) -> Unit
): AutoCloseable
}
}
Input
Parameter | Type | Required in streamUpdates() | Required in streamUpdatesOn() | Default | Description |
---|---|---|---|---|---|
messages | Collection<Message> | No | Yes | n/a | A collection of Message objects for which you want to get updates on changed messages or message reactions. |
callback | (message: T) -> Unit | Yes | No | n/a | Function that takes a single Message object. It defines the custom behavior to be executed when detecting message or message reaction changes. |
callback | messages: (Collection<Message>) -> Unit | No | Yes | n/a | Function that takes a set of Message objects. It defines the custom behavior to be executed when detecting message or message reaction changes. |
Output
Type | Description |
---|---|
AutoCloseable | Interface that lets you stop receiving message-related updates by invoking the close() method. |
Basic usage
-
streamUpdates()
Get message and message reaction-related updates for the message with the timetoken
16200000000000000
published on thesupport
channel.
show all 18 linesval supportChannel: Channel
//...
// fetch the message with timetoken
val timetoken = 16200000000000000L
supportChannel.getMessage(timetoken).async { messageResult ->
messageResult.onSuccess { message ->
// stream updates for the specific message
val autoCloseable = message?.streamUpdates { updatedMessage: Message ->
println("-=Updated message: $updatedMessage")
}
// to stop streaming updates at some later point, use:
// autoCloseable.close()
}.onFailure { error -> -
streamUpdatesOn()
Get message and message reaction-related updates for the first page of messages published on the
support
channel.
show all 23 lines// get the support channel
val supportChannel: Channel
// ...
// fetch the first page of messages
supportChannel.getHistory(count = 25).async { historyResult ->
historyResult.onSuccess { historyResponse ->
val messages = historyResponse.messages
// stream updates for the fetched messages
val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
updatedMessages.forEach { updatedMessage ->
println("-=Updated message: $updatedMessage")
}
}
Other examples
-
streamUpdates()
Stop listening to updates for the message with the timetoken
16200000000000000
published on thesupport
channel.
show all 26 lines// retrieve the support channel
val supportChannel: Channel
// ...
// fetch the message with timetoken
val timetoken = 16200000000000000L
supportChannel.getMessage(timetoken).async { messageResult ->
messageResult.onSuccess { message ->
// stream updates for the specific message
val autoCloseable = message?.streamUpdates<Message> { updatedMessage ->
println("-=Updated message: $updatedMessage")
}
// simulate some processing after which updates are no longer needed
// this could be based on some condition in a real application -
streamUpdatesOn()
Stop listening to updates for the last ten messages published on the
support
channel.
show all 29 lines// get the support channel
val supportChannel: Channel
// ...
// fetch the last ten messages
supportChannel.getHistory(count = 10).async { historyResult ->
historyResult.onSuccess { historyResponse ->
val messages = historyResponse.messages
// stream updates for the fetched messages
val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
updatedMessages.forEach { updatedMessage ->
println("-=Updated message: $updatedMessage")
}
}