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,
completion: ((Swift.Result<MessageImpl, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
newText | String | Yes | n/a | New/updated text that you want to add in place of the existing message. |
Output
Type | Description |
---|---|
MessageImpl | An updated message instance with an added edited action type. |
Basic usage
Correct the number of the support ticket you sent to 78398
.
/// Assuming you have a "chat" instance available
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Timetoken for the message you're trying to get
let timetoken: Timetoken = 16200000000000000
/// Get the message with the specified timetoken
channel.getMessage(timetoken: timetoken) {
switch $0 {
case let .success(message):
if let message = message {
show all 45 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 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
These methods take the following parameters:
-
streamUpdates()
message.streamUpdates(
completion: @escaping ((MessageImpl)?) -> Void
) -> AutoCloseable -
streamUpdatesOn()
(static)Message.streamUpdatesOn(
messages: [MessageImpl],
callback: @escaping (([MessageImpl]) -> Void)
) -> AutoCloseable
Input
Parameter | Type | Required in streamUpdates() | Required in streamUpdatesOn() | Default | Description |
---|---|---|---|---|---|
messages | [MessageImpl] | No | Yes | n/a | A collection of MessageImpl objects for which you want to get updates on changed messages or message reactions. |
completion | @escaping ((MessageImpl)?) -> Void | Yes | No | n/a | Function that takes a single MessageImpl object. It defines the custom behavior to be executed when detecting message or message reaction changes. |
callback | @escaping (([MessageImpl]) -> Void) | No | Yes | n/a | Function that takes a set of MessageImpl objects. It defines the custom behavior to be executed when detecting message or message reaction changes. |
Output
Type | Description |
---|---|
AutoCloseable | Interface that lets you stop receiving message-related updates by invoking the close() method. |
Basic usage
-
streamUpdates()
Get message and message reaction-related updates for the message with the timetoken
16200000000000000
published on thesupport
channel.
show all 36 lines// Assuming you have a "chat" instance and "support" channel available
let timetoken: Timetoken = 16200000000000000
// Fetch the channel metadata
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the message with the specific timetoken on the "support" channel
channel.getMessage(timetoken: timetoken) { result in
switch result {
case let .success(message): -
streamUpdatesOn()
Get message and message reaction-related updates for the first page of messages published on the
support
channel.
show all 41 lines/// Assuming you have a "chat" instance available
// Define properties to hold your channel and auto closeable references
var channel: ChannelImpl?
var streamingAutoCloseable: AutoCloseable?
// Fetch the channel metadata
chat?.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
if let channel = channel {
self?.channel = channel
// Fetch historical messages older than the specified timetoken using only required parameters
let endTimetoken: Timetoken = 15343325214676133
let messageCount: Int = 10
Other examples
-
streamUpdates()
Stop listening to updates for the message with the timetoken
16200000000000000
published on thesupport
channel.
show all 60 lines// Assuming you have a "chat" instance and "support" channel available
let timetoken: Timetoken = 16200000000000000
// Define properties to hold the autoCloseable reference
var autoCloseable: AutoCloseable?
// Fetch the channel metadata
chat?.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the message with the specific timetoken on the "support" channel -
streamUpdatesOn()
Stop listening to updates for the last ten messages published on the
support
channel.
show all 51 lines// Assuming you have a "chat" instance available
// Define properties to hold your channel and auto closeable references
var channel: ChannelImpl?
var autoCloseable: AutoCloseable?
// Fetch the channel metadata
chat?.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
if let channel = channel {
self?.channel = channel
// Fetch the last ten messages on the "support" channel
let endTimetoken: Timetoken = 15343325214676133