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:
message.editText(
newText: String
) async throws -> MessageImpl
Input
Parameter | Description |
---|---|
newText *Type: String Default: n/a | New/updated text that you want to add in place of the existing message. |
Output
Parameter | Description |
---|---|
MessageImpl | An updated message instance with an added edited action type. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Correct the number of the support ticket you sent to 78398
.
// Assuming you have a reference of type "ChatImpl" named "chat".
Task {
if let channel = try await chat.getChannel(channelId: "support") {
// Timetoken for the message you're trying to get
let timetoken: Timetoken = 16200000000000000
// Get the message with the specified timetoken
if let message = try await channel.getMessage(timetoken: timetoken) {
let updatedMessage = try await message.editText(newText: "Your ticket number is 78398")
debugPrint("Message updated successfully")
debugPrint("Updated message: \(updatedMessage)")
} else {
debugPrint("Message not found")
}
} else {
debugPrint("Channel not found")
show all 17 linesGet 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 singleMessage
object.streamUpdatesOn()
checks message and message reaction-related updates on a list ofMessage
objects.
Both methods return an asynchronous stream which produces a new value 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
.
Method signature
These methods take the following parameters:
-
streamUpdates()
message.streamUpdates() -> AsyncStream<MessageImpl>
-
streamUpdatesOn()
(static)MessageImpl.streamUpdatesOn(
messages: [MessageImpl],
) -> AsyncStream<[MessageImpl]>
Input
Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description |
---|---|---|---|
messages Type: [MessageImpl] Default: n/a | No | Yes | A collection of MessageImpl objects for which you want to get updates on changed messages or message reactions. |
Output
An asynchronous stream that produces updates when the underlying message(s) change.
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
-
streamUpdates()
Get message and message reaction-related updates for the message with the timetoken
16200000000000000
published on thesupport
channel.- AsyncStream
- Closure
show all 17 lines// Timetoken for the message you're trying to get
let timetoken: Timetoken = 16200000000000000
// Assuming you have a reference of type "ChatImpl" named "chat".
Task {
if let channel = try await chat.getChannel(channelId: "support") {
// Get the message with the specified timetoken
if let message = try await channel.getMessage(timetoken: timetoken) {
for await updatedMessage in message.streamUpdates() {
debugPrint("Received update for message with timetoken: \(updatedMessage.timetoken)")
}
} else {
debugPrint("Message not found")
}
} else {
debugPrint("Channel not found")// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a reference of type "MessageImpl" named "message"
autoCloseable = message.streamUpdates { updatedMessage in
debugPrint("Received update for message with timetoken: \(updatedMessage.timetoken)")
} -
streamUpdatesOn()
Get message and message reaction-related updates for the first page of messages published on the
support
channel.- AsyncStream
- Closure
show all 17 lines// Assuming you have a reference of type "ChatImpl" named "chat".
Task {
if let channel = try await chat.getChannel(channelId: "support") {
// Fetch the first page of messages
let messages = try await channel.getHistory().messages
// Check if the resulting array is not empty
if !messages.isEmpty {
for await updatedMessage in MessageImpl.streamUpdatesOn(messages: messages) {
messages.forEach { updatedMessage in
debugPrint("Message with timetoken \(updatedMessage.timetoken) updated")
}
}
}
} else {
debugPrint("Channel not found")// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive new updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have an array of "MessageImpl" objects named "messages"
autoCloseable = MessageImpl.streamUpdatesOn(messages: messages) { updatedMessages in
debugPrint("Received updates for messages")
for updatedMessage in updatedMessages {
debugPrint("Message with timetoken \(updatedMessage.timetoken) updated")
// Handle each updated message (e.g., reaction updates, content change)
}
}