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

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

Output

TypeDescription
MessageImplAn 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 lines

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

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

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
messages[MessageImpl]NoYesn/aA collection of MessageImpl objects for which you want to get updates on changed messages or message reactions.
completion@escaping ((MessageImpl)?) -> VoidYesNon/aFunction 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)NoYesn/aFunction that takes a set of MessageImpl objects. It defines the custom behavior to be executed when detecting message or message reaction changes.

Output

TypeDescription
AutoCloseableInterface 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 the support channel.

    // 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):
    show all 36 lines
  • streamUpdatesOn()

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

    /// 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
    show all 41 lines

Other examples

  • streamUpdates()

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

    // 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
    show all 60 lines
  • streamUpdatesOn()

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

    // 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
    show all 51 lines
Last updated on