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 action events on that channel. They can also fetch past message actions 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.

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 action represents.
action.valuestringYesValue which should be stored along with message action.

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 actions in a channel. Returns a list of actions sorted by the action's timetoken in ascending order.

Truncated response

Number of message actions 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 actions.

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.
startstringOptionalMessage action timetoken denoting the start of the range requested. Return values will be less than start.
endstringOptionalMessage action timetoken denoting the end of the range requested. Return values will be greater than or equal to end.
limitnumberOptionalNumber of message actions 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