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(
2    newText: string
3): Promise<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 | 
|---|---|
| Promise<Message> | An updated message instance with an added editedaction type. | 
Sample code
Correct the number of the support ticket you sent to 78398.
1await message.editText(
2    "Your ticket number is 78398"
3)
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- Messageobject.
- streamUpdatesOn()checks message and message reaction-related updates on a list of- Messageobjects.
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(
 2 callback: (message: Message) => unknown
 3): () => void
- 
streamUpdatesOn()1static Message.streamUpdatesOn(
 2 messages: Message[],
 3 callback: (messages: Message[]) => unknown
 4): () => void
Input
| Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description | 
|---|---|---|---|
| messagesType:  Message[]Default: n/a | No | Yes | Array of Messageobjects for which you want to get updates on changed messages or message reactions. | 
| callbackType: n/a Default: n/a | Yes | Yes | Callback function passed as a parameter to both methods. It defines the custom behavior to be executed when detecting message or message reaction changes. | 
| → messageType:  MessageDefault: n/a | Yes | No | Returned Messageobject with the updated message or reactions. | 
| → messagesType:  Message[]Default: n/a | No | Yes | Returned array of Messageobjects with the updated messages or reactions. | 
Output
| Type | Description | 
|---|---|
| () => void | Function you can call to disconnect (unsubscribe) from the channel and stop receiving objectsevents. | 
Errors
Whenever a list of Message objects is required as a parameter, and you try to get updates on messages and message actions without specifying the list, you will receive the Cannot stream message updates on an empty list error.
Sample code
- 
streamUpdates()Get message and message reaction-related updates for the message with the timetoken 16200000000000000published on thesupportchannel.1const channel = await chat.getChannel("support")
 2const message = await channel.getMessage("16200000000000000")
 3message.streamUpdates((message) => {
 4 console.log("Updated message: ", message)
 5})
- 
streamUpdatesOn()Get message and message reaction-related updates for the first page of messages published on the supportchannel.1const channel = await chat.getChannel("support")
 2const { messages } = await channel.getHistory()
 3Message.streamUpdatesOn(messages, (messages) => {
 4 console.log("Updated messages: ", messages)
 5})
Other examples
- 
streamUpdates()Stop listening to updates for the message with the timetoken 16200000000000000published on thesupportchannel.1const channel = await chat.getChannel("support")
 2const message = await channel.getMessage("16200000000000000")
 3const stopUpdates = message.streamUpdates(/* handle update callback */)
 4// after some time...
 5stopUpdates()
- 
streamUpdatesOn()Stop listening to updates for the last ten messages published on the supportchannel.1const channel = await chat.getChannel("support")
 2const { messages } = await channel.getHistory()
 3const stopUpdates = Message.streamUpdatesOn(messages, /* handle updates callback */)
 4// after some time...
 5stopUpdates()
