Using message reactions
Message actions allow users to send back an emoji reaction (for example, a thumbs up or a smiley face) or a custom reaction on a message that they received. These reactions are typically visible to all users in the chat room and displayed with the original message.
User ID / UUID
User ID is also referred to as UUID
/uuid
in some APIs and server responses but holds the value of the userId
parameter you set during initialization.
Add a reaction
Use the addMessageAction
to add a reaction to a message in a channel. You can use the method to add an emoji reaction to a message, or to add any custom reaction as long as it's a string.
You need to provide your own emoji library; this isn't included in the PubNub SDKs.
- JavaScript
- Swift
- Java
- Unity
pubnub.addMessageAction(
{
channel: 'channel1',
messageTimetoken: '15610547826970040',
action: {
type: 'reaction',
value: 'smiley_face',
},
},
function(status, response) {
// handle the response
}
);
extension MyAppMessageAction: MessageAction {}
let action = MyAppMessageAction(type: "reaction", value: "smiley_face")
pubnub.addMessageAction(
channel: "ch-1",
message: action,
messageTimetoken: 15610547826970040
) { 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)")
}
show all 16 linespubnub.addMessageAction()
.channel("ch_1")
.messageAction(new PNMessageAction()
.setType("reaction")
.setValue("smiley_face")
.setMessageTimetoken(15610547826970040L)
)
.async(result -> {
result.onSuccess(res -> {
System.out.println(res.getType());
System.out.println(res.getValue());
System.out.println(res.getUuid());
System.out.println(res.getActionTimetoken());
System.out.println(res.getMessageTimetoken());
}).onFailure(exception -> {
show all 18 linesMessageActionAdd messageAct = new MessageActionAdd();
messageAct.ActionType = "reaction";
messageAct.ActionValue = "smiley_face";
pubnub.AddMessageActions().Channel("ch-1").MessageAction(messageAct).MessageTimetoken(15610547826970040).Async((result, status) => {
if (!status.Error) {
Debug.Log("result.ActionTimetoken: " + result.ActionTimetoken);
Debug.Log("result.ActionType: " + result.ActionType);
Debug.Log("result.ActionValue: " + result.ActionValue);
Debug.Log("result.MessageTimetoken: " + result.MessageTimetoken);
Debug.Log("result.UserID: " + result.UserId);
} else {
Debug.Log(status.Error);
Debug.Log(status.ErrorData.Info);
}
show all 16 linesRemove a reaction
Use the removeMessageAction()
method to remove a reaction from a message by providing an actionTimetoken
and messageTimetoken
for the original message.
- JavaScript
- Swift
- Java
- Unity
pubnub.removeMessageAction(
{
channel: 'ch-1',
messageTimetoken: '15610547826970040',
actionTimetoken: '15610547826999081',
},
function(status, response) {
}
);
pubnub.removeMessageActions(
channel: "ch-1",
message: 15610547826970040,
action: 15610547826999081
) { 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)")
}
})
pubnub.removeMessageAction()
.channel("ch-1")
.messageTimetoken(15610547826970040L)
.actionTimetoken(15610547826999081L)
.async(result -> { /* check result */ });
pubnub.RemoveMessageActions().ActionTimetoken(15610547826999081).Channel("ch-1").MessageTimetoken( .messageTimetoken(15610547826970040L)
).Async((result, status) => {
if (!status.Error) {
// result has no actionable data
} else {
Debug.Log(status.Error);
Debug.Log(status.ErrorData.Info);
}
});
Fetch reactions in channel
Reactions are stored in history alongside messages when they are added by a user.
Use the getMessageActions
method to fetch reactions on a channel. The method works much like other PubNub history methods and allows you to pass a start and end timetoken and fetch all message reactions that were published during that time.
- JavaScript
- Swift
- Objective-C
- Java
- C#
- Python
pubnub.getMessageActions(
{
channel: 'chats.room1',
start: '15901706735798836',
end: '15901706735370016',
limit: 100,
},
function(status, response) {
console.log(status, response);
}
);
pubnub.fetchMessageActions(
channel: "chats.room1") { 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)")
}
})
PNFetchMessageActionsRequest *request =
[PNFetchMessageActionsRequest requestWithChannel:@"chats.room1"];
request.start = @(1234567891);
request.limit = 100;
[self.pubnub fetchMessageActionsWithRequest:request
completion:^(PNFetchMessageActionsResult *result, PNErrorStatus *status) {
if (!status.isError) {
}
else {
}
show all 16 linespubnub.getMessageActions()
.channel("chats.room1")
.async(result -> {
result.onSuccess(res -> {
List<PNMessageAction> actions = res.getActions();
for (PNMessageAction action : actions) {
System.out.println(action.getType());
System.out.println(action.getValue());
System.out.println(action.getUuid());
System.out.println(action.getActionTimetoken());
System.out.println(action.getMessageTimetoken());
}
}).onFailure(exception -> {
exception.printStackTrace();
});
show all 16 linespubnub.GetMessageActions()
.Channel("chats.room1")
.Execute(new PNGetMessageActionsResultExt((result, status) =>
{
}));
Return Type: PNGetMessageActionsResult
{
"MessageActions":[{
"MessageTimetoken":15610547826970040,
"Action":{
"type":"reaction",
"value":"smiley_face"
},
"Uuid":"pn-5903a053-592c-4a1e-8bfd-81d92c962968",
"ActionTimetoken": 15610547826999081
}],
}
pubnub.get_message_actions()
.channel('chats.room1')
.start(15901706735798836) # Some start timetoken or None
.end(15901706735370016) # Some end timetoken or None
.pn_async(message_action_callback)
Fetch messages with reactions
Use the fetchMessages
method with the includeMessageActions
parameter set to fetch past messages in a channel along with reactions that were added to those messages. When you're fetching messages with their reactions, you can only fetch on a single channel at a time.
Message Reaction Events
PubNub triggers events to notify clients when reactions are added to or removed from messages. To receive these events, clients should be subscribed to the channel, and add a messageAction
listener in the code.
Event | Description |
---|---|
Message Reaction Added | Generated when a message action is added to a message. |
Message Reaction Removed | Generated when a message action is removed from a message. |
Message Reaction Added:
{
"channel":"my_channel",
"subscription":null,
"timetoken":"15610547826970040",
"publisher":"user-1",
"message":{
"source":"actions",
"version":"1.0",
"action":"added",
"data":{
"type":"reaction",
"value":"smiley_face",
"messageTimetoken":"15610547826970040",
"actionTimetoken":"15610547826999081"
}
show all 17 linesMessage Reaction Removed:
{
"channel":"my_channel",
"subscription":null,
"timetoken":"15610547826970040",
"publisher":"user-1",
"message":{
"source":"actions",
"version":"1.0",
"action":"removed",
"data":{
"type":"reaction",
"value":"smiley_face",
"messageTimetoken":"15610547826970040",
"actionTimetoken":"15610547826999081"
}
show all 17 lines