Message Actions API for Swift Native SDK

Use message actions to add or remove metadata on published messages. Common uses include receipts and reactions. Clients subscribe to a channel to receive message action events in real time. Clients can also fetch past message actions from Message Persistence independently or when they fetch original messages.

Reactions

"Message Reactions" is a specific application of the Message Actions API for emoji or social reactions.

Message Actions vs. Message Reactions

Message Actions is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while Message Reactions specifically refers to using Message Actions for emoji/social reactions.

In PubNub Core and Chat SDKs, the same underlying Message Actions API is referred to as Message Reactions when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.

Add message action

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action to a published message. The response includes the added action.

Method(s)

Use this Swift method:

func addMessageAction(
channel: String,
type actionType: String,
value: String,
messageTimetoken: Timetoken,
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<PubNubMessageAction, Error>) -> Void)?
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to add the message action to.
type *
Type: String
Default:
n/a
Message action type.
value *
Type: String
Default:
n/a
Message action value.
messageTimetoken *
Type: Timetoken
Default:
n/a
Timetoken of the target message.
custom
Default:
PubNub.RequestConfiguration()
Per-request configuration. See Request Configuration.
completion
Type: ((Result<PubNubMessageAction, Error>) -> Void)?
Default:
nil
Async result of the call.

Completion handler result

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
show all 20 lines

Failure

An Error describing the failure.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Remove message action

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a previously added action from a published message.

Method(s)

Use this Swift method:

func removeMessageActions(
channel: String,
message timetoken: Timetoken,
action actionTimetoken: Timetoken,
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to remove the message action from.
message *
Type: Timetoken
Default:
n/a
Timetoken of the target message.
action *
Type: Timetoken
Default:
n/a
Timetoken of the message action to remove.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
Per-request configuration.
completion
Type: ((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?
Default:
nil
Async result of the call.

Completion handler result

Success

A Tuple containing the channel, message Timetoken, and action Timetoken of the action that was removed.

Failure

An Error describing the failure.

Sample code


Get message actions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.

Truncated response

The number of message actions in the response may be truncated when internal limits are hit. If the response is truncated, a more property is returned with additional parameters. Send iterative calls to Message Persistence, adjusting the parameters to fetch more message actions.

Method(s)

Use this Swift method:

func fetchMessageActions(
channel: String,
page: PubNubBoundedPage? = PubNubBoundedPageBase(),
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<(actions: [PubNubMessageAction], next: PubNubBoundedPage?), Error>) -> Void)?
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to list message actions for.
page
Type: PubNubBoundedPage?
Default:
PubNubBoundedPageBase()
Paging object to specify time bounds; provides start, end, and limit.
custom
Default:
PubNub.RequestConfiguration()
Per-request configuration.
completion
Type: ((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?
Default:
nil
Async result of the call.

Completion handler result

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
show all 20 lines
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.

Sample code


Last updated on