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:
1message.editText(newText: String): PNFuture<Message>
Input
| Parameter | Description |
|---|---|
newText *Type: StringDefault: 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. |
Sample code
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 singleMessageobject.streamUpdatesOn()checks message and message reaction-related updates on a list ofMessageobjects.
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 actions 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()1message.streamUpdates(callback: (message: T) -> Unit): AutoCloseable -
streamUpdatesOn()1class Message {
2 companion object {
3 fun streamUpdatesOn(
4 messages: Collection<Message>,
5 callback: (messages: Collection<Message>) -> Unit
6 ): AutoCloseable
7 }
8}
Input
| Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description |
|---|---|---|---|
messagesType: Collection<Message>Default: n/a | No | Yes | A collection of Message objects for which you want to get updates on changed messages or message reactions. |
callbackType: (message: T) -> UnitDefault: n/a | Yes | No | Function that takes a single Message object. It defines the custom behavior to be executed when detecting message or message reaction changes. |
callbackType: messages: (Collection<Message>) -> UnitDefault: n/a | No | Yes | 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. |
Sample code
-
streamUpdates()Get message and message reaction-related updates for the message with the timetoken
16200000000000000published on thesupportchannel.
show all 18 lines1val supportChannel: Channel
2//...
3
4// fetch the message with timetoken
5val timetoken = 16200000000000000L
6supportChannel.getMessage(timetoken).async { messageResult ->
7 messageResult.onSuccess { message ->
8 // stream updates for the specific message
9 val autoCloseable = message?.streamUpdates { updatedMessage: Message ->
10 println("-=Updated message: $updatedMessage")
11 }
12
13 // to stop streaming updates at some later point, use:
14 // autoCloseable.close()
15 }.onFailure { error -> -
streamUpdatesOn()Get message and message reaction-related updates for the first page of messages published on the
supportchannel.
show all 23 lines1// get the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the first page of messages
6supportChannel.getHistory(count = 25).async { historyResult ->
7 historyResult.onSuccess { historyResponse ->
8 val messages = historyResponse.messages
9
10 // stream updates for the fetched messages
11 val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
12 updatedMessages.forEach { updatedMessage ->
13 println("-=Updated message: $updatedMessage")
14 }
15 }
Other examples
-
streamUpdates()Stop listening to updates for the message with the timetoken
16200000000000000published on thesupportchannel.
show all 26 lines1// retrieve the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the message with timetoken
6val timetoken = 16200000000000000L
7supportChannel.getMessage(timetoken).async { messageResult ->
8 messageResult.onSuccess { message ->
9 // stream updates for the specific message
10 val autoCloseable = message?.streamUpdates<Message> { updatedMessage ->
11 println("-=Updated message: $updatedMessage")
12 }
13
14 // simulate some processing after which updates are no longer needed
15 // this could be based on some condition in a real application -
streamUpdatesOn()Stop listening to updates for the last ten messages published on the
supportchannel.
show all 29 lines1// get the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the last ten messages
6supportChannel.getHistory(count = 10).async { historyResult ->
7 historyResult.onSuccess { historyResponse ->
8 val messages = historyResponse.messages
9
10 // stream updates for the fetched messages
11 val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
12 updatedMessages.forEach { updatedMessage ->
13 println("-=Updated message: $updatedMessage")
14 }
15 }