Message Reactions API for 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.
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 | Description |
---|---|
channel *Type: string | Name of channel which stores the message for which action should be added. |
messageTimetoken *Type: string | Timetoken of message for which action should be added. |
action *Type: Hash | Message action information. |
action.type *Type: string | What feature this message reaction represents. |
action.value *Type: string | Value which should be stored along with message reaction. |
Basic Usage
Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
const PubNub = require('pubnub');
// Initialize PubNub with demo keys
const pubnub = new PubNub({
publishKey: 'demo',
subscribeKey: 'demo',
userId: 'myUniqueUserId'
});
// Function to add a message reaction
async function addReactionToMessage() {
try {
const result = await pubnub.addMessageAction({
channel: "channel1",
messageTimetoken: "15610547826970040", // Replace with actual message timetoken
show all 28 linesReturns
// 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.
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 | Description |
---|---|
channel *Type: string | Name of channel which store message for which action should be removed. |
messageTimetoken *Type: string | Timetoken of message for which action should be removed. |
actionTimetoken *Type: string | 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.
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
})
Parameter | Description |
---|---|
channel *Type: string | Name of channel from which list of messages actions should be retrieved. |
start Type: string | `Message reaction timetoken denoting the start of the range requested. Return values will be less than start. |
end Type: string | Message reaction timetoken denoting the end of the range requested. Return values will be greater than or equal to end. |
limit Type: number | Number 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