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

icon

Under the hood


This method takes the following parameters:

message.editText(
newText: string
): Promise<Message>

Input

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

Output

TypeDescription
Promise<Message>An updated message instance with an added edited action type.

Basic usage

Correct the number of the support ticket you sent to 78398.

await message.editText(
"Your ticket number is 78398"
)

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

icon

Under the hood


These methods take the following parameters:

  • streamUpdates()

    message.streamUpdates(
    callback: (message: Message) => unknown
    ): () => void
  • streamUpdatesOn()

    static Message.streamUpdatesOn(
    messages: Message[],
    callback: (messages: Message[]) => unknown
    ): () => void

Input

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
messagesMessage[]NoYesn/aArray of Message objects for which you want to get updates on changed messages or message reactions.
callbackn/aYesYesn/aCallback function passed as a parameter to both methods. It defines the custom behavior to be executed when detecting message or message reaction changes.
 → messageMessageYesNon/aReturned Message object with the updated message or reactions.
 → messagesMessage[]NoYesn/aReturned array of Message objects with the updated messages or reactions.

Output

TypeDescription
() => voidFunction you can call to disconnect (unsubscribe) from the channel and stop receiving objects events.

Errors

Whenever a list of Message objects is required as a parameter, and you try to get updates on messages and message reactions without specifying the list, you will receive the Cannot stream message updates on an empty list error.

Basic usage

  • streamUpdates()

    Get message and message reaction-related updates for the message with the timetoken 16200000000000000 published on the support channel.

    const channel = await chat.getChannel("support")
    const message = await channel.getMessage("16200000000000000")
    message.streamUpdates((message) => {
    console.log("Updated message: ", message)
    })
  • streamUpdatesOn()

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

    const channel = await chat.getChannel("support")
    const { messages } = await channel.getHistory()
    Message.streamUpdatesOn(messages, (messages) => {
    console.log("Updated messages: ", messages)
    })

Other examples

  • streamUpdates()

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

    const channel = await chat.getChannel("support")
    const message = await channel.getMessage("16200000000000000")
    const stopUpdates = message.streamUpdates(/* handle update callback */)
    // after some time...
    stopUpdates()
  • streamUpdatesOn()

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

    const channel = await chat.getChannel("support")
    const { messages } = await channel.getHistory()
    const stopUpdates = Message.streamUpdatesOn(messages, /* handle updates callback */)
    // after some time...
    stopUpdates()
Last updated on