SwiftSwift Native Message Actions API Reference for Realtime Apps

 
These docs are for the new PubNub Swift SDK written purely in Swift. To view the docs for the older version (the PubNub Objective-C SDK wrapper for Swift), have a look here.

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. ParameterTypeRequiredDescription
    channelStringYesThe name of the channel.
    messageMessageActionYesThe Message Action to associate with a Message.
    messageTimetokenTimetokenYesThe publish timetoken of a parent message.
extension MyAppMessageAction: MessageAction {}

let action = MyAppMessageAction(type: "reaction", value: "smiley_face")

pubnub.addMessageAction(
  channel: "my_channel", 
  message: action,
  messageTimetoken: 15_610_547_826_969_050
) { result in
  switch result {
  case let .success(response):
    print("Successfully Message Action Add Response: \(response)")
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})
public struct MessageActionResponsePayload: Codable, Hashable {
  public let action: MessageActionPayload
  public let error: ErrorPayload?
}

public struct MessageActionPayload: Codable, Hashable {
  public let uuid: String
  public let type: String
  public let value: String
  public let actionTimetoken: Timetoken
  public let messageTimetoken: Timetoken
}

public struct ErrorPayload: Codable, Hashable {
  public let message: EndpointResponseMessage
  public let source: String
  public let details: [ErrorDetail]
}

public struct ErrorDetail: Codable, Hashable, CustomStringConvertible {
  public let message: String
  public let location: String
  public let locationType: String
}

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 peviously added action on a published message. Returns an empty response.

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

  1. ParameterTypeRequiredDescription
    channelStringYesThe name of the channel.
    messageTimetokenYesThe publish timetoken of a parent message.
    actionTimetokenYesThe action timetoken of a message action to be removed.
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("Successfully Message Action Remove Response: \(response)")
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})
public struct DeleteResponsePayload: Codable, Hashable {
  public let error: ErrorPayload?
}

public struct ErrorPayload: Codable, Hashable {
  public let message: EndpointResponseMessage
  public let source: String
  public let details: [ErrorDetail]
}

public struct ErrorDetail: Codable, Hashable, CustomStringConvertible {
  public let message: String
  public let location: String
  public let locationType: String
}

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
    channelStringYes The name of the channel.
    startTimetoken?OptionalnilAction timetoken denoting the start of the range requested (exclusive).
    endTimetoken?OptionalnilAction timetoken denoting the end of the range requested (inclusive).
    limitInt?OptionalnilThe max number of message actions to retrieve per request.
pubnub.fetchMessageActions(
  channel: String,
) { result in
  switch result {
  case let .success(response):
    print("Successfully Message Action Fetch Response: \(response)")
  case let .failure(error):
    print("Error from failed response: \(error.localizedDescription)")
  }
})
public struct MessageActionsResponsePayload: Codable, Hashable {
  public let actions: [MessageActionPayload]
  public let start: Timetoken?
  public let end: Timetoken?
  public let limit: Int?
}

public struct MessageActionPayload: Codable, Hashable {
  public let uuid: String
  public let type: String
  public let value: String
  public let actionTimetoken: Timetoken
  public let messageTimetoken: Timetoken
}