Presence API for C# SDK
Presence enables you to track the online and offline status of users and devices in real time and store custom state information. Presence provides authoritative information on:
- When a user has joined or left a channel
- Who, and how many, users are subscribed to a particular channel
- Which channel(s) an individual user is subscribed to
- Associated state information for these users
Learn more about our Presence feature here.
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}");
}
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the HereNow()
function in your application.
Cache
This method has a 3 second response cache time.
Method(s)
To call Here Now
you can use the following method(s) in the C# SDK:
pubnub.HereNow()
.Channels(Array)
.ChannelGroups(Array)
.IncludeState(bool)
.IncludeUUIDs(bool)
.QueryParam(Dictionary<string,object>)
Parameter | Description |
---|---|
Channels Type: Array | The Channels to get the here now details. |
ChannelGroups Type: Array | The ChannelGroups to get the here now details. |
IncludeState Type: bool | If true , the response will include the presence states of the users for Channels /ChannelGroups . |
IncludeUUIDs Type: bool | If true , the response will include the UUIDs of the connected clients. |
QueryParam Type: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
Async Type: | PNCallback of type PNHereNowResult .This parameter is deprecated and will be removed in a future version. Please use the ExecuteAsync parameter instead. |
Execute *Type: PNCallback | PNCallback of type PNHereNowResult . |
ExecuteAsync Type: None | Returns PNResult<PNHereNowResult> . |
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.
Get a list of UUIDs subscribed to channel
Returns
The HereNow()
operation returns a PNResult<PNHereNowResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNHereNowResult | Returns a PNHereNowResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNHereNowResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
TotalChannels | int | Total Channels . |
TotalOccupancy | int | Total Occupancy . |
Channels | Dictionary<string, PNHereNowChannelData> | A map with values of PNHereNowChannelData for each channel . See PNHereNowChannelData for more details. |
PNHereNowChannelData
Property Name | Type | Description |
---|---|---|
ChannelName | string | Channel name. |
Occupancy | int | Occupancy of the channel . |
Occupants | List<PNHereNowOccupantData> | A list of PNHereNowOccupantData , see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
Property Name | Type | Description |
---|---|---|
Uuid | string | UUIDs of the user. |
State | object | State of the user. |
Other examples
Get a list of UUIDs subscribed to channel synchronously
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Example response
{
"status" : 200,
"message" : "OK",
"service" : "Presence",
"uuids" : [
{
"uuid" : "myUUID0"
},
{
"state" : {
"abcd" : {
"age" : 15
}
},
"uuid" : "myUUID1"
show all 38 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can return only the occupancy
information for a single channel by specifying the channel and setting UUIDs
to false:
Example response
{
"status": 200,
"message": "OK",
"payload": {
"channels": {
"81d8d989-b95f-443c-a726-04fac323b331": {
"uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ],
"occupancy": 1
},
"81b7a746-d153-49e2-ab70-3037a75cec7e": {
"uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
"occupancy": 1
},
"c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
"uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
show all 23 linesWhere now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can obtain information about the current list of channels to which a UUID is subscribed to by calling the WhereNow()
function in your application.
Timeout events
If the app is killed/crashes and restarted (or the page containing the PubNub instance is refreshed on the browser) within the heartbeat window no timeout event is generated.
Method(s)
To call WhereNow()
you can use the following method(s) in the C# SDK:
pubnub.WhereNow()
.Uuid(string)
.QueryParam(Dictionary<string,object>)
Parameter | Description |
---|---|
Uuid *Type: string | Uuid . |
QueryParam Type: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
Async Type: | PNCallback of type PNWhereNowResult .This parameter is deprecated and will be removed in a future version. Please use the ExecuteAsync parameter instead. |
Execute *Type: Command | PNCallback of type PNWhereNowResult . |
ExecuteAsync Type: None | Returns PNResult<PNWhereNowResult> . |
Sample code
You simply need to define the uuid
and the callback
function to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to
Returns
The WhereNow()
operation returns a PNResult<PNWhereNowResult>
which contain the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNWhereNowResult | Returns a PNWhereNowResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNWhereNowResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Channels | List<string> | The list of channels where the UUID is present. |
Other examples
Get a list of channels synchronously
Obtain information about the current list of channels of some other UUID
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
The state API is used to set/get key/value pairs specific to a subscriber Uuid
.
State information is supplied as a Generic Dictionary object(Dictionary<string, object>
) of key/value pairs.
Method(s)
Set state
pubnub.SetPresenceState()
.Channels(Array)
.ChannelGroups(Array)
.State(Dictionary<string, object>)
.Uuid(string)
.QueryParam(Dictionary<string,object>)
Parameter | Description |
---|---|
Channels Type: Array | Channels to set state . |
ChannelGroups Type: Array | ChannelGroups to set state . |
State Type: Dictionary <string, object> | State to set. |
Uuid *Type: string | Uuid |
QueryParam Type: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
Async Type: | PNCallback of type PNSetStateResult .This parameter is deprecated and will be removed in a future version. Please use the ExecuteAsync parameter instead. |
Execute *Type: PNCallback | PNCallback of type PNSetStateResult . |
ExecuteAsync Type: None | Returns PNResult<PNSetStateResult> . |
Get state
pubnub.GetPresenceState()
.Channels(Array)
.ChannelGroups(Array)
.Uuid(string)
.QueryParam(Dictionary<string,object>)
Parameter | Description |
---|---|
Channels Type: Array | Channel name to fetch the state . |
ChannelGroups Type: Array | ChannelGroups name to fetch the state . |
Uuid *Type: string | Uuid |
QueryParam Type: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
Async Type: | PNCallback of type PNGetStateResult .This parameter is deprecated and will be removed in a future version. Please use the ExecuteAsync parameter instead. |
Execute *Type: PNCallback | PNCallback of type PNGetStateResult . |
ExecuteAsync Type: None | Returns PNResult<PNGetStateResult> . |
Sample code
Set state
Get state
Returns
The SetPresenceState()
operation returns a PNResult<PNSetStateResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNGetStateResult | Returns a PNSetStateResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNSetStateResult
contains the following property:
Property Name | Type | Description |
---|---|---|
State | Dictionary<string, object> | Dictionary of UUIDs and the user states. |
The GetPresenceState()
operation returns a PNResult<PNGetStateResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNGetStateResult | Returns a PNGetStateResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNGetStateResult
contains the following property:
Property Name | Type | Description |
---|---|---|
StateByUUID | Dictionary<string, object> | Dictionary of UUIDs and the user states. |
Other examples
Set state synchronously
Get state synchronously
Set state for channels in channel group
The above code would return the following response to the client:
{
first : "Robert",
last : "Plant",
age : 59,
region : "UK"
}