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

Message Actions vs. Message Reactions

Message Actions is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while Message Reactions specifically refers to using Message Actions for emoji/social reactions.

In PubNub Core and Chat SDKs, the same underlying Message Actions API is referred to as Message Reactions when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.

Add Message Action

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 Action, you can use the following method(s) in the JavaScript SDK:

addMessageAction({
channel: string,
messageTimetoken: string,
action: {type: string, value: string}
})
* required
ParameterDescription
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 action represents.
action.value *
Type: string
Value which should be stored along with message action.

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 action
async function addReactionToMessage() {
try {
const result = await pubnub.addMessageAction({
channel: "channel1",
messageTimetoken: "15610547826970040", // Replace with actual message timetoken
show all 28 lines

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 Action

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 Action, you can use the following method(s) in the JavaScript SDK:

removeMessageAction({
channel: string,
messageTimetoken: string,
actionTimetoken: string
})
* required
ParameterDescription
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 Actions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

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 Actions, you can use the following method(s) in the JavaScript SDK:

getMessageActions({
channel: string,
start: string,
end: string,
limit: number
})
* required
ParameterDescription
channel *
Type: string
Name of channel from which list of messages actions should be retrieved.
start
Type: string
`Message action timetoken denoting the start of the range requested. Return values will be less than start.
end
Type: string
Message action timetoken denoting the end of the range requested. Return values will be greater than or equal to end.
limit
Type: number
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
Last updated on