Message Reactions API for C# SDK
Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message reaction events on that channel. They can also fetch past message reactions from Message Persistence independently or when they fetch original messages.
Terminology
All message reactions in code are collectively called message reactions in the docs and the term applies both to reacting to a message (through emojis) and acting on a message (by deleting it).
Request execution
We recommend using try
and catch
statements when working with the C# SDK.
If there's an issue with the provided API parameter values, like missing a required parameter, the SDK throws an exception. However, if there is a server-side API execution issue or a network problem, the error details are contained within the status
.
try
{
PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
.Message("Why do Java developers wear glasses? Because they can't C#.")
.Channel("my_channel")
.ExecuteAsync();
PNStatus status = publishResponse.Status;
Console.WriteLine("Server status code : " + status.StatusCode.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
}
Add Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Add an action on a published message
. Returns PNAddMessageActionResult
in the response.
Method(s)
To Add a Message Reaction you can use the following method(s) in the C# SDK:
pubnub.AddMessageAction()
.Channel(string)
.MessageTimetoken(long)
.Action(PNMessageAction)
Parameter | Description |
---|---|
Channel *Type: string | Specifies channel name to publish message reactions to. |
MessageTimetoken *Type: long | Timestamp when the actual message was created the message reaction belongs to. |
Action *Type: PNMessageAction | Specify the action you want to publish of type PNMessageAction . |
PNMessageAction
Parameter | Description |
---|---|
Type *Type: string | Message reaction's type . |
Value *Type: string | Message reaction's value . |
Basic Usage
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.
using System;
using PubnubApi;
class AddMessageActionExample
{
static void Main(string[] args)
{
// Configuration
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
SubscribeKey = "demo",
PublishKey = "demo",
Secure = true
};
show all 42 linesReturns
{
"MessageTimetoken":15610547826969050,
"ActionTimetoken":15610547826970050,
"Action":{
"type":"reaction",
"value":"smiley_face"
},
"Uuid":"user-456"
}
Remove Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Remove a previously added action on a published message
. Returns an empty response.
Method(s)
To Remove a Message Reaction you can use the following method(s) in the C# SDK:
pubnub.RemoveMessageAction()
.Channel(string)
.MessageTimetoken(long)
.ActionTimetoken(long)
.Uuid(string)
Parameter | Description |
---|---|
Channel *Type: string | Specifies channel name to publish message reactions to. |
MessageTimetoken *Type: long | Publish timetoken of the original message |
ActionTimetoken *Type: long | Publish timetoken of the message reaction to be removed. |
Uuid *Type: string | UUID of the message . |
Basic Usage
pubnub.RemoveMessageAction()
.Channel("my_channel")
.MessageTimetoken(15701761818730000)
.ActionTimetoken(15701775691010000)
.Uuid("mytestuuid")
.Execute(new PNRemoveMessageActionResultExt((result, status) =>
{
//empty result of type PNRemoveMessageActionResult.
}));
Returns
The RemoveMessageAction()
operation returns a no actionable data.
Get Message Reactions
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Get a list of message reactions in a channel
. Returns a list of actions sorted by the action's timetoken in ascending order.
Truncated response
Number of message reactions in the response may be truncated when internal limits are hit. If the response is truncated, a more
property will be returned with additional parameters. Send iterative calls to Message Persistence adjusting the parameters to fetch more message reactions.
Method(s)
To Get Message Reactions you can use the following method(s) in the C# SDK:
pubnub.GetMessageActions()
.Channel(string)
.Start(long)
.End(long)
.Limit(int)
Parameter | Description |
---|---|
Channel *Type: string Default: n/a | The channel name. |
Start Type: long Default: n/a | Message Reaction timetoken denoting the start of the range requested (return values will be less than start ). |
End Type: long Default: n/a | Message Reaction timetoken denoting the end of the range requested (return values will be greater than or equal to end ). |
Limit Type: int Default: 100 | Specifies the number of actions to return in response. Default/Maximum is 100 . |
Basic Usage
pubnub.GetMessageActions()
.Channel("my_channel")
.Execute(new PNGetMessageActionsResultExt((result, status) =>
{
//result is of type PNGetMessageActionsResult.
}));
Returns
{
"MessageActions":
[{
"MessageTimetoken":15610547826969050,
"Action":{
"type":"reaction",
"value":"smiley_face"
},
"Uuid":"pn-5903a053-592c-4a1e-8bfd-81d92c962968",
"ActionTimetoken":15717253483027900
}],
"More": {
"Start": 15610547826970050,
"End": 15645905639093361,
"Limit": 2
show all 17 lines