On this page

Message Actions API for C# 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.

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.

Request execution

Use try/catch when working with the C# SDK.

If a request has invalid parameters (for example, a missing required field), the SDK throws an exception. If the request reaches the server but fails (server error or network issue), the error details are available in the returned status.

1try
2{
3 PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
4 .Message("Why do Java developers wear glasses? Because they can't C#.")
5 .Channel("my_channel")
6 .ExecuteAsync();
7
8 PNStatus status = publishResponse.Status;
9
10 Console.WriteLine("Server status code : " + status.StatusCode.ToString());
11}
12catch (Exception ex)
13{
14 Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
15}

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 C# method:

1pubnub.AddMessageAction()
2 .Channel(string)
3 .MessageTimetoken(long)
4 .Action(PNMessageAction)
* required
ParameterDescription
Channel *
Type: string
Channel name to add the message action to.
MessageTimetoken *
Type: long
Timetoken of the target message.
Action *Message action payload.

PNMessageAction

* required
ParameterDescription
Type *
Type: string
Message action type.
Value *
Type: string
Message action value.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
1

Returns

1{
2 "MessageTimetoken":15610547826969050,
3 "ActionTimetoken":15610547826970050,
4 "Action":{
5 "type":"reaction",
6 "value":"smiley_face"
7 },
8 "Uuid":"user-456"
9}

Remove 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 C# method:

1pubnub.RemoveMessageAction()
2 .Channel(string)
3 .MessageTimetoken(long)
4 .ActionTimetoken(long)
5 .Uuid(string)
* required
ParameterDescription
Channel *
Type: string
Channel name to remove the message action from.
MessageTimetoken *
Type: long
Timetoken of the target message.
ActionTimetoken *
Type: long
Timetoken of the message action to remove.
Uuid *
Type: string
UUID of the message.

Sample code

1

Returns

The RemoveMessageAction() operation returns no actionable 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.

How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

Method(s)

Use this C# method:

1pubnub.GetMessageActions()
2 .Channel(string)
3 .Start(long)
4 .End(long)
5 .Limit(int)
* required
ParameterDescription
Channel *
Type: string
Default:
n/a
Channel name to list message actions for.
Start
Type: long
Default:
n/a
Newer boundary of the query (exclusive). Must be a higher timetoken value than End when both are provided.
End
Type: long
Default:
n/a
Older boundary of the query (inclusive). Must be a lower timetoken value than Start when both are provided.
Limit
Type: int
Default:
100
Maximum number of actions to return. Default/Maximum is 100.

Sample code

1

Returns

1{
2"MessageActions":
3 [{
4 "MessageTimetoken":15610547826969050,
5 "Action":{
6 "type":"reaction",
7 "value":"smiley_face"
8 },
9 "Uuid":"pn-5903a053-592c-4a1e-8bfd-81d92c962968",
10 "ActionTimetoken":15717253483027900
11 }],
12"More": {
13 "Start": 15610547826970050,
14 "End": 15645905639093361,
15 "Limit": 2
show all 17 lines