Message Actions

With the Message Actions feature, you can add actions to messages that are already published. These actions can be useful for delivery acknowledgments, read receipts, and actions such as emojis. You can provide any custom action as long as it's a string. You'll have to use your own emoji library as this isn't included in the PubNub SDKs.

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 actions

Add an action on a message (returns the added action in the response).

pubnub.addMessageAction(
{
channel: 'chats.room1',
messageTimetoken: '15610547826970040',
action: {
type: 'reaction',
value: 'smiley_face',
}
},
function(status, response) {
console.log(status, response);
}
);

Receive message actions

To receive an action on a message, the receiving client should be listening to an event of type messageAction, and should subscribe to a channel in which the message action is being added. There is no need to subscribe any differently to a channel for receiving a message action.

Remove message actions

You may need to remove a previously added action on a message (returns an empty response).

pubnub.removeMessageAction(
{
channel: 'chats.room1',
messageTimetoken: '15610547826970040',
actionTimetoken: '15610547826970075',
},
function(status, response) {

});

Retrieve actions

You can retrieve messages and the associated message actions using History with Actions API. You can also just retrieve the message actions that were written in the given time interval. This can be used to retrieve all actions posted since the app went offline. Getting a list of message actions in a channel returns a list of actions sorted by the message action's timetoken from the oldest to the latest.

Actions

Message actions can be posted on messages that were published long before the time interval in which the app was offline.

pubnub.getMessageActions(
{
channel: 'chats.room1',
start: '15610547826970041',
end: '15610547826970040',
limit: 100,
},
function(status, response) {
console.log(status, response);
}
);

Delete messages

There is a setting to allow delete from history requests which you must enable by checking the Enable Delete-From-History for your PubNub API Keys in the Admin Portal. In the following example, the start and end timetoken parameter values are 1/10 nanosecond (last digit of timetoken) apart to delete the message stored at the end parameter's timetoken value.

pubnub.deleteMessages(
{
channels: 'chats.room1',
start: "15526611838554309",
end: "15526611838554310",
},
function (status, response) {
console.log(status, response);
}
);

Bulk message delete

You can also delete multiple messages or all the messages stored in a channel.

ParametersBehavior
start & end
Delete messages between those timetokens (only one message [at end timetoken] if values differ by 1)
start only
Delete all messages before (not at) that timetoken
end only
Delete all messages after (and at) that timetoken
none
Delete ALL messages
No undo option

There is no undo for the Delete Message API.

Last updated on