Add message actions

Showing JavaScript examples.

This guide shows you how to attach a message action to a message PubNub already delivered, such as an emoji reaction, a read receipt, or a custom annotation.

  • Add an action with a type and a value to an existing message.
  • Confirm the action was stored from the response PubNub returns.
  • Recognize the one status code that means the action was written but its event wasn't published.

Every call on this page needs an SDK instance initialized with your publish key. It also targets a message that already exists, identified by that message's timetoken, the value a successful publish call returned. Examples use the JavaScript, Swift, Java, Kotlin, C#, Python, and Objective-C SDKs. For any other language, refer to Available SDKs.

Before adding a message action, confirm the prerequisites are in place.

A message action is a standard PubNub API request, so it's subject to the same size limit as a publish call.

ItemLimitNotes
Request sizeSame limit as Publish.A message action is sent as a standard PubNub API request, so the publish request-body limit applies.
type and value fieldsNo dedicated limitBounded only by the overall request size.

Add an action to a message​

Call the add-action method with the channel, the target message's timetoken, and the type/value pair you're attaching.

1const response = await pubnub.addMessageAction({
2 channel: 'chats.room1',
3 messageTimetoken: '15610547826969050',
4 action: {
5 type: 'reaction',
6 value: 'smiley_face',
7 },
8});
9
10console.log('actionTimetoken:', response.data.actionTimetoken);

A successful call returns the action PubNub stored:

1{
2 "type": "reaction",
3 "value": "smiley_face",
4 "uuid": "user-456",
5 "actionTimetoken": "15610547826970050",
6 "messageTimetoken": "15610547826969050"
7}

Keep the returned actionTimetoken. Removing this action later needs it, and it isn't derivable from the message's own timetoken.

Handle a 207 status​

A 207 status means PubNub stored the action but couldn't publish its Message Action event to subscribers. Treat the action as added. Don't call add again for the same reaction or receipt. That creates a second, duplicate action instead of retrying the first one. If your code depends on subscribers seeing it, have them catch up with Retrieve historical message actions instead.

Was this page useful?

Last updated on