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:

1func addMessageAction(
2 channel: String,
3 type actionType: String,
4 value: String,
5 messageTimetoken: Timetoken,
6 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
7 completion: ((Result<PubNubMessageAction, Error>) -> Void)?
8)
required
channel required#
Type: String
Default: no default
Channel name to add the message action to.
type required#
Type: String
Default: no default
Message action type.
value required#
Type: String
Default: no default
Message action value.
messageTimetoken required#
Type: Timetoken
Default: no default
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.

1public protocol PubNubMessageAction {
2
3 /// The type of action
4 var actionType: String { get }
5
6 /// The value that corresponds to the type
7 var actionValue: String { get }
8
9 /// The `Timetoken` for this specific action
10 var actionTimetoken: Timetoken { get }
11
12 /// The `Timetoken` for the message this action relates to
13 var messageTimetoken: Timetoken { get }
14
15 /// The publisher of the message action
show all 20 lines

Failure​

An Error describing the failure.

Sample code​

1import PubNubSDK
2import Foundation
3
4// Initializes a PubNub object with the configuration
5let pubnub = PubNub(
6 configuration: PubNubConfiguration(
7 publishKey: "demo",
8 subscribeKey: "demo",
9 userId: "myUniqueUserId"
10 )
11)
12
13// Add a message action to already existing message published at a specific timetoken
14pubnub.addMessageAction(
15 channel: "my_channel",
show all 27 lines

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:

1func removeMessageActions(
2 channel: String,
3 message timetoken: Timetoken,
4 action actionTimetoken: Timetoken,
5 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
6 completion: ((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?
7)
required
channel required#
Type: String
Default: no default
Channel name to remove the message action from.
message required#
Type: Timetoken
Default: no default
Timetoken of the target message.
action required#
Type: Timetoken
Default: no default
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​

1// Remove a message action from a message. Both the message and the action are identified by their timetokens
2pubnub.removeMessageActions(
3 channel: "my_channel",
4 message: 15_610_547_826_969_050,
5 action: 15_610_547_826_970_050
6) { result in
7 switch result {
8 case let .success(response):
9 print("Action published at \(response.action) was removed from message \(response.message) on channel \(response.channel)")
10 case let .failure(error):
11 print("Error from failed response: \(error.localizedDescription)")
12 }
13}

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.

How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

Method(s)​

Use this Swift method:

1func fetchMessageActions(
2 channel: String,
3 page: PubNubBoundedPage? = PubNubBoundedPageBase(),
4 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
5 completion: ((Result<(actions: [PubNubMessageAction], next: PubNubBoundedPage?), Error>) -> Void)?
6)
required
channel required#
Type: String
Default: no default
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).

1public protocol PubNubMessageAction {
2
3 /// The type of action
4 var actionType: String { get }
5
6 /// The value that corresponds to the type
7 var actionValue: String { get }
8
9 /// The `Timetoken` for this specific action
10 var actionTimetoken: Timetoken { get }
11
12 /// The `Timetoken` for the message this action relates to
13 var messageTimetoken: Timetoken { get }
14
15 /// The publisher of the message action
show all 20 lines
1public protocol PubNubBoundedPage {
2
3 /// The start value for the next set of remote data
4 var start: Timetoken? { get }
5
6 /// The bounded end value that will be eventually fetched to
7 var end: Timetoken? { get }
8
9 /// The previous limiting value (if any)
10 var limit: Int? { get }
11}

Failure​

An Error describing the failure.

Sample code​

1// Fetch all message actions for a channel
2pubnub.fetchMessageActions(channel: "my_channel") { result in
3 switch result {
4 case let .success(response):
5 print("The actions for the channel \(response.actions)")
6 print("The next page used for pagination: \(String(describing: response.next))")
7 case let .failure(error):
8 print("Error from failed response: \(error.localizedDescription)")
9 }
10}

Was this page useful?

Last updated on