Add message actions
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
typeand avalueto 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.
| Item | Limit | Notes |
|---|---|---|
| Request size | Same limit as Publish. | A message action is sent as a standard PubNub API request, so the publish request-body limit applies. |
type and value fields | No dedicated limit | Bounded 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.
- JavaScript
- Swift
- Java
- Kotlin
- C#
- Python
- Objective-C
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);
1pubnub.addMessageAction(
2 channel: "chats.room1",
3 type: "reaction",
4 value: "smiley_face",
5 messageTimetoken: 15610547826969050
6) { result in
7 switch result {
8 case let .success(action):
9 print("actionTimetoken: \(action.actionTimetoken)")
10 case let .failure(error):
11 print("failed: \(error.localizedDescription)")
12 }
13}
1PNMessageAction messageAction = new PNMessageAction()
2 .setType("reaction")
3 .setValue("smiley_face")
4 .setMessageTimetoken(15610547826969050L);
5
6pubnub.addMessageAction()
7 .channel("chats.room1")
8 .messageAction(messageAction)
9 .async(result -> result.onSuccess(res ->
10 System.out.println("actionTimetoken: " + res.getActionTimetoken())));
1val messageAction = PNMessageAction().apply {
2 type = "reaction"
3 value = "smiley_face"
4 messageTimetoken = 15610547826969050L
5}
6
7pubnub.addMessageAction(
8 channel = "chats.room1",
9 messageAction = messageAction
10).async { result ->
11 result.onSuccess { println("actionTimetoken: ${it.actionTimetoken}") }
12}
1pubnub.AddMessageAction()
2 .Channel("chats.room1")
3 .MessageTimetoken(15610547826969050)
4 .Action(new PNMessageAction { Type = "reaction", Value = "smiley_face" })
5 .Execute(new PNAddMessageActionResultExt((result, status) => {
6 if (result != null) {
7 Console.WriteLine("actionTimetoken: " + result.ActionTimetoken);
8 }
9 }));
1msg_action = PNMessageAction()
2msg_action.type = "reaction"
3msg_action.value = "smiley_face"
4msg_action.message_timetoken = "15610547826969050"
5
6def add_action_callback(result, status):
7 if status.is_error():
8 print(status.error_data.information)
9 else:
10 print("actionTimetoken:", result.action_timetoken)
11
12pubnub.add_message_action() \
13 .channel("chats.room1") \
14 .message_action(msg_action) \
15 .pn_async(add_action_callback)
1PNAddMessageActionRequest *request =
2 [PNAddMessageActionRequest requestWithChannel:@"chats.room1"
3 messageTimetoken:@15610547826969050];
4request.type = @"reaction";
5request.value = @"smiley_face";
6
7[self.client addMessageActionWithRequest:request completion:^(PNAddMessageActionStatus *status) {
8 if (!status.isError) {
9 NSLog(@"actionTimetoken: %@", status.data.action.actionTimetoken);
10 }
11}];
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.
Related tasks
- Message Actions in PubNub. The type/value model, the event model, and the Message Persistence requirement.
- Receive message actions. Handle the event this call generates on a live subscription.
- Retrieve historical message actions. Fetch actions for a channel, or catch up on one added while a client was offline.
- Remove message actions. Delete a previously added action using the
actionTimetokenthis call returns. - Send different message types. Publish the message an action attaches to.