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 reactions such as emojis. You can provide any custom reaction 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.

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);
}
);

Receiving 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.

Removing 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) {

});

Retrieving 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

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);
}
);

Deleting 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 & endDelete messages between those timetokens (only one message [at end timetoken] if values differ by 1)
start onlyDelete all messages before (not at) that timetoken
end onlyDelete all messages after (and at) that timetoken
noneDelete ALL messages
No undo option

There is no undo for the Delete Message API.

Last updated on