Message Reactions API for PubNub JavaScript SDK

Add or remove actions 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.

Supported and recommended asynchronous patterns

PubNub supports Callbacks, Promises, and Async/Await for asynchronous JS operations. The recommended pattern is Async/Await and all sample requests in this document are based on it. This pattern returns a status only on detecting an error. To receive the error status, you must add the try...catch syntax to your code.

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 JavaScript SDK:

addMessageAction({
channel: string,
messageTimetoken: string,
action: {type: string, value: string}
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelstringYesName of channel which stores the message for which action should be added.
messageTimetokenstringYesTimetoken of message for which action should be added.
actionHashYesMessage action information.
action.typestringYesWhat feature this message reaction represents.
action.valuestringYesValue which should be stored along with message reaction.

Basic Usage

try {
const result = await pubnub.addMessageAction({
channel: "channel1",
messageTimetoken: "15610547826970040",
action: {
type: "reaction",
value: "smiley_face",
},
});
} catch (status) {
console.log(status);
}

Returns

// Example of status
{
"error": false,
"operation": "PNAddMessageActionOperation",
"statusCode": 200
}

// Example of response
{
"data": {
"type": "reaction",
"value": "smiley_face",
"uuid": "user-456",
"actionTimetoken": "15610547826970050",
"messageTimetoken": "15610547826969050"
show all 17 lines

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. Returns an empty response.

Method(s)

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

removeMessageAction({
channel: string,
messageTimetoken: string,
actionTimetoken: string
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelstringYesName of channel which store message for which action should be removed.
messageTimetokenstringYesTimetoken of message for which action should be removed.
actionTimetokenstringYesAction addition timetoken.

Basic Usage

try {
const result = await pubnub.removeMessageAction({
channel: "channel1",
messageTimetoken: "15610547826970040",
actionTimetoken: "15610547826970040",
});
} catch (status) {
console.log(status);
}

Returns

// Example of status
{
"error": false,
"operation": "PNRemoveMessageActionOperation",
"statusCode": 200
}

// Example of response
{
"data": {}
}

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 JavaScript SDK:

getMessageActions({
channel: string,
start: string,
end: string,
limit: number
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelstringYesName of channel from which list of messages actions should be retrieved.
startstringOptional`Message reaction timetoken denoting the start of the range requested. Return values will be less than start.
endstringOptionalMessage reaction timetoken denoting the end of the range requested. Return values will be greater than or equal to end.
limitnumberOptionalNumber of message reactions to return in response.

Basic Usage

try {
const result = await pubnub.getMessageActions({
channel: "channel1",
start: "15610547826970041",
end: "15610547826970040",
limit: 100,
});
} catch (status) {
console.log(status);
}

Returns

// Example of status
{
"error": false,
"operation": "PNGetMessageActionsOperation",
"statusCode": 200
}

// Example of response
{
"data": [
{
"type": "reaction",
"value": "smiley_face",
"uuid": "user-456",
"actionTimetoken": "15610547826970050",
show all 21 lines
Last updated on