Manage message updates
Edit messages and receive real-time update events.
Requires Message Persistence
Enable Message Persistence in the Admin Portal.
Edit messages
editText() replaces an existing message's content.
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 edited action 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
Receive real-time updates when messages or reactions change:
streamUpdates()- updates for a singleMessageobjectstreamUpdatesOn()- updates for multipleMessageobjects
Both return an unsubscribe function.
Stream update behavior
streamUpdates()returns the entire updatedMessageon each changestreamUpdatesOn()returns the complete list of monitored messages on each change
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 Message objects 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 Message object with the updated message or reactions. |
→ messagesType: Message[]Default: n/a | No | Yes | Returned array of Message objects with the updated messages or reactions. |
Output
| Type | Description |
|---|---|
() => void | Function 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 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 // The callback receives the entire updated Message object (including all reactions)
5 // each time a change occurs.
6 console.log("Updated message: ", message)
7}) -
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 // The callback receives the complete list of all messages you're monitoring
5 // each time any change occurs.
6 console.log("Updated messages: ", messages)
7})
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()