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}
})
Parameter | Type | Required | Description |
---|---|---|---|
Operation Arguments | Hash | Yes | A hash of arguments. |
channel | string | Yes | Name of channel which stores the message for which action should be added. |
messageTimetoken | string | Yes | Timetoken of message for which action should be added. |
action | Hash | Yes | Message action information. |
action.type | string | Yes | What feature this message action represents. |
action.value | string | Yes | Value 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 linesRemove 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
})
Parameter | Type | Required | Description |
---|---|---|---|
Operation Arguments | Hash | Yes | A hash of arguments. |
channel | string | Yes | Name of channel which store message for which action should be removed. |
messageTimetoken | string | Yes | Timetoken of message for which action should be removed. |
actionTimetoken | string | Yes | Action 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
})
Parameter | Type | Required | Description |
---|---|---|---|
Operation Arguments | Hash | Yes | A hash of arguments. |
channel | string | Yes | Name of channel from which list of messages actions should be retrieved. |
start | string | Optional | Message action timetoken denoting the start of the range requested. Return values will be less than start. |
end | string | Optional | Message action timetoken denoting the end of the range requested. Return values will be greater than or equal to end. |
limit | number | Optional | Number 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