Reactions

Add, get, and delete message reactions for messages sent on apps built with the Chat SDK.

Add & delete

toggleReaction() is a method for both adding and removing message reactions. It adds a string flag to the message if the current user hasn't added it yet or removes it if the current user already added it before.

If you use this method to add or remove message reactions, this flag would be a literal emoji you could implement in your app's UI. However, you could also use this method for a different purpose, like marking a message as pinned to a channel or unpinned if you implement the pinning feature in your chat app.

Method signature

This method takes the following parameters:

message.toggleReaction(
reaction: String
) async throws -> MessageImpl

Input

* required
ParameterDescription
reaction *
Type: String
Default:
n/a
Emoji added to the message or removed from it by the current user.

Output

ParameterDescription
MessageImpl
Updated message instance with an added reactions action type.

Basic usage

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Add the "thumb up" emoji (\u{1F44D}) to the last message on the support channel.

// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
if let message = try await channel.getHistory(count: 1).messages.first {
let updatedMessage = try await message.toggleReaction(reaction: "\u{1F44D}")
debugPrint("Reaction added successfully to message: \(updatedMessage)")
} else {
debugPrint("No messages found in history")
}
} else {
debugPrint("Channel not found")
}
}

Get updates

To learn how to receive updates whenever a message reaction is added, edited, or removed on other clients, head to the Get updates section.

Get reactions for one message

You can access the reactions property of the Message object to return a list of all reactions added to the given messages (with the ID of the user who added it and the timetoken stating when this action was added).

Basic usage

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

List all reactions added to the last message on the support channel.

// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
if let message = try await channel.getHistory(count: 1).messages.first {
debugPrint("Reactions for the latest message: \(message.reactions)")
} else {
debugPrint("No messages found in history")
}
} else {
debugPrint("Channel not found")
}
}

Get historical reactions

If you have Message Persistence enabled on your keyset, PubNub stores all historical info about messages, their metadata, and reactions.

If you want to fetch historical info about message reactions, use the getHistory() method. By default, when you fetch historical messages, PubNub returns all message reactions and metadata attached to the retrieved messages.

Check

hasUserReaction() checks if the current user added a given emoji to the message.

Method signature

This method takes the following parameters:

message.hasUserReaction(reaction: String) -> Bool

Input

* required
ParameterDescription
reaction *
Type: String
Default:
n/a
Specific emoji added to the message.

Output

ParameterDescription
Bool
Specifies if the current user added a given emoji to the message or not.

Basic usage

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Check if the current user added the "thumb up" emoji (👍) to the last message on the support channel.

// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
if let message = try await channel.getHistory(count: 1).messages.first {
if message.hasUserReaction(reaction: "\u{1F44D}") {
print("The current user has added the 'thumb up' emoji (👍) to the latest message.")
} else {
print("The current user has not added the 'thumb up' emoji (👍) to the latest message.")
}
} else {
debugPrint("No messages found in history")
}
} else {
debugPrint("Channel not found")
}
show all 16 lines
Last updated on