Presence API for PubNub 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.

Here Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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>)
ParameterTypeRequiredDescription
ChannelsArrayOptionalThe Channels to get the here now details.
ChannelGroupsArrayOptionalThe ChannelGroups to get the here now details.
IncludeStateboolOptionalIf true, the response will include the presence states of the users for Channels/ChannelGroups.
IncludeUUIDsboolOptionalIf true, the response will include the UUIDs of the connected clients.
QueryParamDictionary<string, object>OptionalDictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncPNCallbackDeprecatedPNCallback of type PNHereNowResult.
ExecutePNCallbackYesPNCallback of type PNHereNowResult.
ExecuteAsyncNoneOptionalReturns PNResult<PNHereNowResult>.

Basic Usage

Get a list of UUIDs subscribed to channel

PNResult<PNHereNowResult> herenowResponse = await pubnub.HereNow()
// tailor the next two lines to example
.Channels(new string[] {
"coolChannel",
"coolChannel2"
})
.IncludeUUIDs(true)
.ExecuteAsync();
PNHereNowResult herenowResult = herenowResponse.Result;
PNStatus status = herenowResponse.Status;
if (status.Error)
{
// handle error
return;
}
show all 36 lines

Returns

The HereNow() operation returns a PNResult<PNHereNowResult> which contains the following properties:

Property NameTypeDescription
ResultPNHereNowResultReturns a PNHereNowResult object.
StatusPNStatusReturns a PNStatus object.

PNHereNowResult contains the following properties:

Property NameTypeDescription
TotalChannelsintTotal Channels.
TotalOccupancyintTotal Occupancy.
ChannelsDictionary<string, PNHereNowChannelData>A map with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details.

PNHereNowChannelData

Property NameTypeDescription
ChannelNamestringChannel name.
OccupancyintOccupancy of the channel.
OccupantsList<PNHereNowOccupantData>A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details.

PNHereNowOccupantData

Property NameTypeDescription
UuidstringUUIDs of the user.
StateobjectState of the user.

Other Examples

Get a list of UUIDs subscribed to channel synchronously

pubnub.HereNow()
// tailor the next two lines to example
.Channels(new string[] {
"coolChannel",
"coolChannel2"
})
.IncludeUUIDs(true)
.Execute(new PNHereNowResultEx(
(result, status) => {
if (status.Error) {
// handle error
return;
}

if (result.Channels != null && result.Channels.Count > 0) {
show all 33 lines

Returning State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

PNResult<PNHereNowResult> herenowResponse = await pubnub.HereNow()
.Channels(new string[] {
// who is present on those channels?
"my_channel"
})
.IncludeState(true) // include state with request (false by default)
.IncludeUUIDs(true) // if false, only shows occupancy count
.ExecuteAsync();

PNHereNowResult herenowResult = herenowResponse.Result;
PNStatus status = herenowResponse.Status;
//handle it

Example Response

{
"status" : 200,
"message" : "OK",
"service" : "Presence",
"uuids" : [
{
"uuid" : "myUUID0"
},
{
"state" : {
"abcd" : {
"age" : 15
}
},
"uuid" : "myUUID1"
show all 38 lines

Return Occupancy Only

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:

PNResult<PNHereNowResult> herenowResponse = await pubnub.HereNow()
.Channels(new string[] {
// who is present on those channels?
"my_channel"
})
.IncludeState(false) // include state with request (false by default)
.IncludeUUIDs(false) // if false, only shows occupancy count
.ExecuteAsync();

PNHereNowResult herenowResult = herenowResponse.Result;
PNStatus status = herenowResponse.Status;
//handle it

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 lines

Where Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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>)
ParameterTypeRequiredDescription
UuidstringYesUuid.
QueryParamDictionary<string, object>OptionalDictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncCommandDeprecatedPNCallback of type PNWhereNowResult.
ExecuteCommandYesPNCallback of type PNWhereNowResult.
ExecuteAsyncNoneOptionalReturns PNResult<PNWhereNowResult>.

Basic Usage

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

PNResult<PNWhereNowResult> wherenowResponse = await pubnub.WhereNow()
.ExecuteAsync();

PNWhereNowResult wherenowResult = wherenowResponse.Result;
PNStatus status = wherenowResponse.Status;
// returns a pojo with channels
// channel groups which I am part of.

Returns

The WhereNow() operation returns a PNResult<PNWhereNowResult> which contain the following properties:

Property NameTypeDescription
ResultPNWhereNowResultReturns a PNWhereNowResult object.
StatusPNStatusReturns a PNStatus object.

PNWhereNowResult contains the following properties:

Property NameTypeDescription
ChannelsList<string>The list of channels where the UUID is present.

Other Examples

Get a list of channels synchronously

pubnub.WhereNow()
.Execute(new PNWhereNowResultExt(
(result, status) => {
// returns a pojo with channels
// channel groups which I am part of.
}
));

Obtain information about the current list of channels of some other UUID

PNResult<PNWhereNowResult> wherenowResponse = await pubnub.WhereNow()
.Uuid("some-other-uuid") // uuid of the user we want to spy on.
.ExecuteAsync();

PNWhereNowResult wherenowResult = wherenowResponse.Result;
PNStatus status = wherenowResponse.Status;
// returns a pojo with channels
// channel groups which "some-other-uuid" part of.ere_now_example_1

User State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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>)
ParameterTypeRequiredDescription
ChannelsArrayOptionalChannels to set state.
ChannelGroupsArrayOptionalChannelGroups to set state.
StateDictionary<string, object>OptionalState to set.
UuidstringYesUuid
QueryParamDictionary<string, object>OptionalDictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncPNCallbackDeprecatedPNCallback of type PNSetStateResult.
ExecutePNCallbackYesPNCallback of type PNSetStateResult.
ExecuteAsyncNoneOptionalReturns PNResult<PNSetStateResult>.

Get State

pubnub.GetPresenceState()
.Channels(Array)
.ChannelGroups(Array)
.Uuid(string)
.QueryParam(Dictionary<string,object>)
ParameterTypeRequiredDescription
ChannelsArrayOptionalChannel name to fetch the state.
ChannelGroupsArrayOptionalChannelGroups name to fetch the state.
UuidstringYesUuid
QueryParamDictionary<string, object>OptionalDictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncPNCallbackDeprecatedPNCallback of type PNGetStateResult.
ExecutePNCallbackYesPNCallback of type PNGetStateResult.
ExecuteAsyncNoneOptionalReturns PNResult<PNGetStateResult>.

Basic Usage

Set State

Dictionary<string, object> myState = new Dictionary<string, object>();
myState.Add("age", 20);

PNResult<PNSetStateResult> setstateResponse = await pubnub.SetPresenceState()
.Channels(new string[] {
"ch1",
"ch2",
"ch3"
})
.State(myState)
.ExecuteAsync();

PNSetStateResult setstateResult = setstateResponse.Result;
PNStatus status = setstateResponse.Status;
// handle set state response

Get State

PNResult<NGetStateResult> getstateResponse = await pubnub.GetPresenceState()
.Channels(new string[] {
// channels to fetch state for
"ch1",
"ch2",
"ch3"
})
.ChannelGroups(new string[] {
// channel groups to fetch state for
"cg1",
"cg2",
"cg3"
})
.Uuid("suchUUID") // uuid of user to fetch, or for own uuid
.ExecuteAsync();
show all 19 lines

Returns

The SetPresenceState() operation returns a PNResult<PNSetStateResult> which contains the following properties:

Property NameTypeDescription
ResultPNGetStateResultReturns a PNSetStateResult object.
StatusPNStatusReturns a PNStatus object.

PNSetStateResult contains the following property:

Property NameTypeDescription
StateDictionary<string, object>Dictionary of UUIDs and the user states.

The GetPresenceState() operation returns a PNResult<PNGetStateResult> which contains the following properties:

Property NameTypeDescription
ResultPNGetStateResultReturns a PNGetStateResult object.
StatusPNStatusReturns a PNStatus object.

PNGetStateResult contains the following property:

Property NameTypeDescription
StateByUUIDDictionary<string, object>Dictionary of UUIDs and the user states.

Other Examples

Set state synchronously

Dictionary<string, object> myState = new Dictionary<string, object>();
myState.Add("age", 20);

pubnub.SetPresenceState()
.Channels(new string[] {
"ch1",
"ch2",
"ch3"
})
.State(myState)
.Execute(new PNSetStateResultExt(
(result, status) => {
// handle set state response
}
));

Get state synchronously

pubnub.GetPresenceState()
.Channels(new string[] {
// channels to fetch state for
"ch1",
"ch2",
"ch3"
})
.ChannelGroups(new string[] {
// channel groups to fetch state for
"cg1",
"cg2",
"cg3"
})
.Uuid("suchUUID") // uuid of user to fetch, or for own uuid
.Execute(new PNGetStateResultExt(
show all 19 lines

Set state for channels in channel group

Dictionary<string, object> myState = new Dictionary<string, object>();
myState.Add("age", 20);

PNResult<PNSetStateResult> setstateResponse = await pubnub.SetPresenceState()
.ChannelGroups(new string[] {
// apply on those channel groups
"cg1",
"cg2",
"cg3"
})
.Channels(new string[] {
// apply on those channels
"ch1",
"ch2",
"ch3"
show all 22 lines

The above code would return the following response to the client:

{
first : "Robert",
last : "Plant",
age : 59,
region : "UK"
}
Last updated on