Using message reactions

Message reactions 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.

pubnub.addMessageAction(
{
channel: 'channel1',
messageTimetoken: '15610547826970040',
action: {
type: 'reaction',
value: 'smiley_face',
},
},
function(status, response) {
// handle the response
}
);

Remove a reaction

Use the removeMessageAction() method to remove a reaction from a message by providing an actionTimetoken and messageTimetoken for the original message.

pubnub.removeMessageAction(
{
channel: 'ch-1',
messageTimetoken: '15610547826970040',
actionTimetoken: '15610547826999081',
},
function(status, response) {

}
);

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.

pubnub.getMessageActions(
{
channel: 'chats.room1',
start: '15901706735798836',
end: '15901706735370016',
limit: 100,
},
function(status, response) {
console.log(status, response);
}
);

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.

EventDescription
Message Reaction AddedGenerated when a message reaction is added to a message.
Message Reaction RemovedGenerated when a message reaction 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 lines

Message 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
Last updated on