Message Actions API for JavaScript SDK
Use message actions to add or remove metadata on published messages. Common uses include receipts and reactions. Clients subscribe to a channel to receive message action events. Clients can also fetch past message actions from Message Persistence, either on demand or when fetching 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.
Reactions
"Message Reactions" is a specific application of the Message Actions API for emoji or social reactions.
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
Enable Message Persistence for your key in the Admin Portal as described in the support article.
Add an action to a published message. The response includes the added action.
Method(s)
Use this JavaScript method:
addMessageAction({
channel: string,
messageTimetoken: string,
action: {type: string, value: string}
})
Parameter | Description |
---|---|
channel *Type: string | Channel name of the target message. |
messageTimetoken *Type: string | Timetoken of the target message. |
action *Type: Hash | Message action payload. |
action.type *Type: string | Message action type. |
action.value *Type: string | Message action value. |
Sample code
Reference code
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 action
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal as described in the support article.
Remove a previously added action from a published message. The response is empty.
Method(s)
Use this JavaScript method:
removeMessageAction({
channel: string,
messageTimetoken: string,
actionTimetoken: string
})
Parameter | Description |
---|---|
channel *Type: string | Channel name of the target message. |
messageTimetoken *Type: string | Timetoken of the target message. |
actionTimetoken *Type: string | Timetoken of the message action to remove. |
Sample code
Returns
// Example of status
{
"error": false,
"operation": "PNRemoveMessageActionOperation",
"statusCode": 200
}
// Example of response
{
"data": {}
}
Get message actions
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal as described in the support article.
Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.
Truncated response
The number of message actions in the response may be truncated when internal limits are hit. If the response is truncated, a more
property is returned with additional parameters. Send iterative calls to Message Persistence, adjusting the parameters to fetch more message actions.
Method(s)
Use this JavaScript method:
getMessageActions({
channel: string,
start: string,
end: string,
limit: number
})
Parameter | Description |
---|---|
channel *Type: string | Channel name to list message actions for. |
start Type: string | Message action timetoken for the start of the range (exclusive). |
end Type: string | Message action timetoken for the end of the range (inclusive). |
limit Type: number | Number of message actions to return. |
Sample code
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