Message Reactions API for PubNub Swift Native SDK

Add or remove reactions 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 reaction events on that channel. They can also fetch past message reactions from Message Persistence independently or when they fetch original messages.

Terminology

All message reactions in code are collectively called message reactions in the docs and the term applies both to reacting to a message (through emojis) and acting on a message (by deleting it).

Add Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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

Method(s)

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

addMessageAction(
channel: String,
type actionType: String,
value: String,
messageTimetoken: Timetoken,
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<PubNubMessageAction, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesThe name of the channel
typeStringYesThe Message Reaction's type
valueStringYesThe Message Reaction'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

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 reaction
var publisher: String { get }
show all 19 lines

Failure

An Error describing the failure.

Basic Usage

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)")
}
})

Remove Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Remove a previously added action on a published message.

Method(s)

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

removeMessageActions(
channel: String,
message timetoken: Timetoken,
action actionTimetoken: Timetoken,
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(channel: String, message: Timetoken, action: Timetoken), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesThe name of the channel.
messageTimetokenYesThe publish timetoken of a parent message.
actionTimetokenYesThe action timetoken of a message reaction 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

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.

Basic Usage

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)")
}
})

Get Message Reactions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Get a list of message reactions in a channel. Returns a list of actions sorted by the action's timetoken in ascending order.

Truncated response

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

Method(s)

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

fetchMessageActions(
channel: String,
page: PubNubBoundedPage? = PubNubBoundedPageBase(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(actions: [PubNubMessageAction], next: PubNubBoundedPage?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesThe name of the channel.
pagePubNubBoundedPage?OptionalPubNubBoundedPageBase()The paging object used for pagination. It allows you to specify a range of messages to retrieve based on specific time bounds.
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

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 reaction
var publisher: String { get }
show all 19 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.

Basic Usage

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)")
}
})
Last updated on