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

ParameterTypeRequiredDefaultDescription
newTextStringYesn/aNew/updated text that you want to add in place of the existing message.

Output

TypeDescription
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 single Message object.
  • streamUpdatesOn() checks message and message reaction-related updates on a list of Message 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

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
messagesCollection<Message>NoYesn/aA collection of Message objects for which you want to get updates on changed messages or message reactions.
callback(message: T) -> UnitYesNon/aFunction that takes a single Message object. It defines the custom behavior to be executed when detecting message or message reaction changes.
callbackmessages: (Collection<Message>) -> UnitNoYesn/aFunction that takes a set of Message objects. It defines the custom behavior to be executed when detecting message or message reaction changes.

Output

TypeDescription
AutoCloseableInterface 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 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 { updatedMessage: Message ->
    println("-=Updated message: $updatedMessage")
    }

    // to stop streaming updates at some later point, use:
    // autoCloseable.close()
    }.onFailure { error ->
    show all 18 lines
  • streamUpdatesOn()

    Get message and message reaction-related updates for the first page of messages published on the support channel.

    // 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")
    }
    }
    show all 23 lines

Other examples

  • streamUpdates()

    Stop listening to updates for the message with the timetoken 16200000000000000 published on the support channel.

    // 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
    show all 26 lines
  • streamUpdatesOn()

    Stop listening to updates for the last ten messages published on the support channel.

    // 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")
    }
    }
    show all 29 lines
Last updated on