SwiftSwift Native Message Actions API Reference for Realtime Apps

 
The PubNub Swift 3.0 SDK contains many significant changes from the 2.x SDK, including breaking changes. Please refer to the PubNub Swift 3.0 Migration Guide for more details.

Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message action events on that channel. They can also fetch past message actions from PubNub Storage independently or when they fetch original messages.


Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Add an action on a published message. Returns the added action in the response.

To Add a Message Action you can use the following method(s) in the Swift SDK:

  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesThe name of the channel
    typeStringYesThe Message Action's type
    valueStringYesThe Message Action's value
    messageTimetokenTimetokenYesThe publish timetoken of a parent message.
    customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
    completion((Result<PubNubMessageAction, Error>) -> Void)?OptionalnilThe async Result of the method call
  • Success: The PubNubMessageAction that was added.

    public protocol PubNubMessageAction {
      /// The type of action
      var actionType: String { get }
    
      /// The value that corresponds to the type
      var actionValue: String { get }
    
      /// The `Timetoken` for this specific action
      var actionTimetoken: Timetoken { get }
    
      /// The `Timetoken` for the message this action relates to
      var messageTimetoken: Timetoken { get }
    
      /// The publisher of the message action
      var publisher: String { get }
    
      /// The channel the action (and message) were sent on
      var channel: String { get }
    }
  • Failure: An Error describing the failure.
pubnub.addMessageAction(
  channel: "my_channel",
  type: "reaction",
  value: "smiley_face",
  messageTimetoken: 15_610_547_826_969_050
) { result in
  switch result {
  case let .success(messageAction):
    print("Action type \(action.actionType) added at \(action.actionTimetoken) with value \(action.actionValue)")
    print("`\(action.publisher)` added action onto message \(action.messageTimetoken) on `\(action.channel)`")
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})

Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Remove a previously added action on a published message.

To Remove a Message Action you can use the following method(s) in the Swift SDK:

  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesThe name of the channel.
    messageTimetokenYesThe publish timetoken of a parent message.
    actionTimetokenYesThe action timetoken of a message action to be removed.
    customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
    completion((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?OptionalnilThe async Result of the method call
  • Success: A Tuple containing the channel, message Timetoken, and action Timetoken of the action that was removed.
  • Failure: An Error describing the failure.
pubnub.removeMessageActions(
  channel: "my_channel",
  message: 15_610_547_826_969_050,
  action: 15_610_547_826_970_050
) { result in
  switch result {
  case let .success(response):
    print("Action published at \(response.action) was removed from message \(response.message) on channel \(response.channel)")
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})

Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Get a list of message actions in a channel. Returns a list of actions in the response.

To Get Message Actions you can use the following method(s) in the Swift SDK:

  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesThe name of the channel.
    pagePubNubBoundedPage?OptionalPubNubBoundedPageBase()The paging object used for pagination
    customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
    completion((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?OptionalnilThe async Result of the method call
  • Success: An Array of PubNubMessageAction for the request channel, and the next request PubNubBoundedPage (if one exists).

    public protocol PubNubMessageAction {
      /// The type of action
      var actionType: String { get }
    
      /// The value that corresponds to the type
      var actionValue: String { get }
    
      /// The `Timetoken` for this specific action
      var actionTimetoken: Timetoken { get }
    
      /// The `Timetoken` for the message this action relates to
      var messageTimetoken: Timetoken { get }
    
      /// The publisher of the message action
      var publisher: String { get }
    
      /// The channel the action (and message) were sent on
      var channel: String { get }
    }
    public protocol PubNubBoundedPage {
      /// The start value for the next set of remote data
      var start: Timetoken? { get }
    
      /// The bounded end value that will be eventually fetched to
      var end: Timetoken? { get }
    
      /// The previous limiting value (if any)
      var limit: Int? { get }
    }
  • Failure: An Error describing the failure.
pubnub.fetchMessageActions(
  channel: String,
) { result in
  switch result {
  case let .success(response):
    print("The actions for the channel \(response.actions)")
    if let nextPage = response.next {
      print("The next page used for pagination: \(nextPage)")
    }
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})