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
newTextrequired#- 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 a Message object changes using the event-driven method:
onUpdated()- fires when a message's content or reactions change
The method accepts a callback and returns an unsubscribe function.
Method signature
1message.onUpdated(
2 callback: (message: Message) => void
3): () => void
Input
callbackrequired#- Callback function invoked when the message changes.
- property of callback:
messagerequired# - The updated
Messageobject.
Output
| Type | Description |
|---|---|
() => void | Function you can call to stop listening to message updates. |
Sample code
Listen for updates on a specific message.
1const channel = await chat.getChannel("support")
2const message = await channel.getMessage("16200000000000000")
3
4const stopUpdated = message.onUpdated((updated) => {
5 console.log("Message updated:", updated.content.text)
6})
7
8// after some time...
9stopUpdated()
Get message updates (deprecated)
Deprecated
streamUpdates() is deprecated. Use onUpdated() instead. streamUpdatesOn() remains supported.
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
messages#- Array of
Messageobjects for which you want to get updates on changed messages or message reactions.- Required in streamUpdates()
- No
- Required in streamUpdatesOn()
- Yes
callback#- Callback function passed as a parameter to both methods. It defines the custom behavior to be executed when detecting message or message reaction changes.
- Required in streamUpdates()
- Yes
- Required in streamUpdatesOn()
- Yes
- property of callback:
message# - Returned
Messageobject with the updated message or reactions.- Required in streamUpdates()
- Yes
- Required in streamUpdatesOn()
- No
- property of callback:
messages# - Returned array of
Messageobjects with the updated messages or reactions.- Required in streamUpdates()
- No
- Required in streamUpdatesOn()
- Yes
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()