App Context API for C# SDK

This page describes App Context (formerly Objects v2). To upgrade from Objects v1, refer to the migration guide.

App Context provides easy-to-use, serverless storage for user and channel data that you need to build reliable, scalable applications. Use App Context to store metadata about your application users and channels, and their membership associations, without the need to run your own databases.

PubNub triggers events when object data is set, updated, or removed. Setting the same data that already exists does not trigger an event. Clients can receive these events in real time and update the front end accordingly.

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.

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}");
}

User

Get metadata for all users

Returns a paginated list of UUID Metadata objects, optionally including the custom data object for each.

Method(s)

To Get All UUID Metadata you can use the following method(s) in the C# SDK:

pubnub.GetAllUuidMetadata()
.IncludeCustom(bool)
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
.Filter(string)
.Limit(int)
* required
ParameterDescription
IncludeCustom
Type: bool
Whether to include the Custom object in the response.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.

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.

Response

{
"Uuids": [
{
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": null,
"ProfileUrl": null,
"Custom": null,
"Updated": "2020-06-17T16:28:14.060718Z"
},
{
"Uuid": "uuid-2",
"Name": "Bob Cat",
"Email": "bobc@example.com",
show all 29 lines

Get user metadata

Returns metadata for the specified UUID, optionally including the custom data object for each.

Method(s)

To Get UUID Metadata you can use the following method(s) in the C# SDK:

pubnub.GetUuidMetadata()
.Uuid(string)
.IncludeCustom(bool)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
IncludeCustom
Type: bool
Whether to include the Custom object in the response.

Sample code


Response

{
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": null,
"ProfileUrl": null,
"Custom": null,
"Updated": "2020-06-17T16:28:14.060718Z"
}

Set user metadata

Unsupported partial updates of custom metadata

The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. Set the entire updated custom object.

Set metadata for a UUID in the database, optionally including the custom data object for each.

Method(s)

To Set UUID Metadata you can use the following method(s) in the C# SDK:

pubnub.SetUuidMetadata()
.Uuid(string)
.Name(string)
.Email(string)
.ExternalId(string)
.ProfileUrl(string)
.Custom(Dictionary<string, object>)
.IncludeCustom(bool)
.IfMatchesEtag(string)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
Name *
Type: string
Display name for the user.
Email
Type: string
The user's email address.
ExternalId
Type: string
User's identifier in an external system.
ProfileUrl
Type: string
The URL of the user's profile picture.
Custom
Type: Dictionary<string, object>
Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported.
IncludeCustom
Type: bool
Whether to include the Custom object in the response.
IfMatchesEtag
Type: String
Use the eTag from an applicable get metadata call to ensure updates only apply if the object hasn’t changed. If the eTags differ, the server returns HTTP 412.
API limits

To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.

Sample code


Response

{
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": null,
"ProfileUrl": null,
"Custom": null,
"Updated": "2020-06-17T16:28:14.060718Z"
}

Remove user metadata

Removes the metadata from a specified UUID.

Method(s)

To Remove UUID Metadata you can use the following method(s) in the C# SDK:

pubnub.RemoveUuidMetadata()
.Uuid(string)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.

Sample code


Response

{}

Channel

Get metadata for all channels

Returns a paginated list of Channel Metadata objects, optionally including the custom data object for each.

Method(s)

To Get All Channel Metadata you can use the following method(s) in the C# SDK:

pubnub.GetAllChannelMetadata()
.IncludeCustom(bool)
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
.Filter(string)
* required
ParameterDescription
IncludeCustom
Type: bool
Whether to include the Custom object in the response.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.

Sample code


Response

{
"Channels": [
{
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:52:19.562469Z"
},
{
"Channel": "main",
"Name": "Main channel",
"Description": "The main channel",
"Custom": {
"public": true,
show all 26 lines

Get channel metadata

Returns metadata for the specified Channel, optionally including the custom data object for each.

Method(s)

To Get Channel Metadata you can use the following method(s) in the C# SDK:

pubnub.GetChannelMetadata()
.Channel(string)
.IncludeCustom(bool)
* required
ParameterDescription
Channel *
Type: string
Channel name.
IncludeCustom
Type: bool
Whether to include the Custom object in the response.

Sample code


Response

{
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:52:19.562469Z"
}

Set channel metadata

Unsupported partial updates of custom metadata

The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. Set the entire updated custom object.

Set metadata for a Channel in the database, optionally including the custom data object for each.

Method(s)

To Set Channel Metadata you can use the following method(s) in the C# SDK:

pubnub.SetChannelMetadata()
.Channel(string)
.Name(string)
.Description(string)
.Custom(Dictionary<string, object>)
.IncludeCustom(bool)
.IfMatchesEtag(string)
* required
ParameterDescription
Channel *
Type: string
Channel name.
Name
Type: string
Channel name.
Description
Type: string
Channel description.
Custom
Type: Dictionary<string, object>
Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported.
IncludeCustom
Type: bool
Whether to include the Custom object in the response.
IfMatchesEtag
Type: String
Use the eTag from an applicable get metadata call to ensure updates only apply if the object hasn’t changed. If the eTags differ, the server returns HTTP 412.
API limits

To learn about the maximum length of parameters used to set channel metadata, refer to REST API docs.

Sample code


Response

{
"Channel": "my-channel",
"Name": "John Doe",
"Description": "sample description",
"Custom": {
"color": "blue"
},
"Updated": "2020-06-17T16:52:19.562469Z"
}

Other examples

Iteratively update existing metadata

Remove channel metadata

Removes the metadata from a specified channel.

Method(s)

To Remove Channel Metadata you can use the following method(s) in the C# SDK:

pubnub.RemoveChannelMetadata()
.Channel(string)
* required
ParameterDescription
Channel *
Type: string
Channel name.

Sample code


Response

{}

Channel memberships

Get channel memberships

This method returns a list of channel memberships for a user. This method doesn't return subscriptions.

Method(s)

To Get Memberships you can use the following method(s) in the C# SDK:

pubnub.GetMemberships()
.Uuid(string)
.Include(PNMembershipField[])
.IncludeCount(bool)
.Page(PNPageObject)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
Include
Type: PNMembershipField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.

Sample code


Response

{
"Memberships": [
{
"ChannelMetadata": {
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:55:44.632042Z"
},
"Custom": {
"starred": false
},
"Updated": "2020-06-17T17:05:25.987964Z"
},
show all 38 lines

Set channel memberships

Set channel memberships for a UUID.

Method(s)

To Set Memberships you can use the following method(s) in the C# SDK:

pubnub.SetMemberships()
.Uuid(string)
.Channels(List<PNMembership>)
.Include(PNMembershipField[])
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
.Filter(string)
.Limit(int)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
Channels *Channels to add to the membership. Accepts channel names (strings) or objects (with optional custom data).
Include
Type: PNMembershipField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.
API limits

To learn about the maximum length of parameters used to set channel membership metadata, refer to REST API docs.

PNMembership

* required
PropertyDescription
Channel *
Type: string
The name of the channel associated with this membership.
Custom
Type: Dictionary<string, object>
A dictionary that stores custom metadata related to the membership, allowing for additional context or information. Values can be strings, numbers, or booleans.
Status
Type: string
The status of the membership, for example: "active" or "inactive"
Type
Type: string
The type of membership for categorization purposes.

Sample code


Response

{
"Memberships": [
{
"ChannelMetadata": {
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:55:44.632042Z"
},
"Custom": {
"starred": false
},
"Updated": "2020-06-17T17:05:25.987964Z"
},
show all 38 lines

Remove channel memberships

Remove channel memberships for a UUID.

Method(s)

To Remove Memberships you can use the following method(s) in the C# SDK:

pubnub.RemoveMemberships()
.Uuid(string)
.Channels(List<string>)
.Include(PNMembershipField[])
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
.Filter(string)
.Limit(int)
* required
ParameterDescription
Uuid
Type: String
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
Channels *
Type: List<string>
Channels to remove from membership.
Include
Type: PNMembershipField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.

Sample code


Response

{
"Memberships": [
{
"ChannelMetadata": {
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:55:44.632042Z"
},
"Custom": {
"starred": false
},
"Updated": "2020-06-17T17:05:25.987964Z"
},
show all 38 lines

Manage channel memberships

Set and remove channel memberships for a user.

Method(s)

To Manage Memberships you can use the following method(s) in the C# SDK:

pubnub.ManageMemberships()
.Uuid(string)
.Set(List<PNMembership>)
.Remove(List<string>)
.Include(PNMembershipField[])
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
* required
ParameterDescription
Uuid *
Type: string
Unique user identifier (UUID). If not supplied, the current user's UUID is used.
SetSet channel memberships for the user.
Remove
Type: List<string>
Remove channel memberships for the user.
Include
Type: PNMembershipField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).

Sample code


Response

{
"Memberships": [
{
"ChannelMetadata": {
"Channel": "my-channel",
"Name": "My channel",
"Description": "A channel that is mine",
"Custom": null,
"Updated": "2020-06-17T16:55:44.632042Z"
},
"Custom": {
"starred": false
},
"Updated": "2020-06-17T17:05:25.987964Z"
},
show all 38 lines

Channel members

Get channel members

This method returns a list of members in a channel. The list includes user metadata for members that have additional metadata stored in the database.

Method(s)

To Get Channel Members you can use the following method(s) in the C# SDK:

pubnub.GetChannelMembers()
.Channel(string)
.Include(PNChannelMemberField[])
.IncludeCount(bool)
.Page(PNPageObject)
.Sort(List<string>)
.Filter(string)
.Limit(int)
* required
ParameterDescription
Channel *
Type: string
Channel name.
Include
Type: PNChannelMemberField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.

Sample code


Response

{
"ChannelMembers": [
{
"UuidMetadata": {
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": "",
"ProfileUrl": "",
"Custom": null,
"Updated": "2019-02-20T23:11:20.89375"
},
"Custom": {
"role": "admin"
},
show all 39 lines

Set channel members

This method sets members in a channel.

Method(s)

To Set Channel Members you can use the following method(s) in the C# SDK:

pubnub.SetChannelMembers().Channel(string).Uuids(List<PNChannelMember>).Include(PNChannelMemberField[]).Page(PNPageObject).Sort(List<string>).Filter(string).Limit(int)
* required
ParameterDescription
Channel *
Type: String
Channel name.
Uuids *
Type: List<PNChannelMember>
Members to add to the channel. Accepts UUIDs (strings) or objects (with optional custom data).
Include
Type: PNChannelMemberField[]
Whether to include additional fields.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.
API limits

To learn about the maximum length of parameters used to set channel members metadata, refer to REST API docs.

Sample code


Response

{
"ChannelMembers": [
{
"UuidMetadata": {
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": "",
"ProfileUrl": "",
"Custom": null,
"Updated": "2019-02-20T23:11:20.89375"
},
"Custom": {
"role": "admin"
},
show all 39 lines

Remove channel members

Remove members from a Channel.

Method(s)

To Remove Channel Members you can use the following method(s) in the C# SDK:

pubnub.RemoveChannelMembers()
.Channel(string)
.Remove( List)
.Include(PNChannelMembersInclude[])
.Limit(int)
.Count(bool)
.Start(string)
.End(string)
.Sort(List)
.Async()
* required
ParameterDescription
Channel *
Type: string
Channel name.
Uuids *
Type: List<string>
Members to remove from the channel.
Include
Type: PNChannelMemberField[]
Whether to include additional fields.
IncludeCount
Type: bool
Whether to include the total count in the paginated response. Default is false.
Page
Type: PNPageObject
Cursor-based pagination.
Sort
Type: List<string>
Sort by id, name, updated with asc/desc (for example, {name: 'asc'}).
Filter
Type: string
Filter expression. Only matching objects are returned. See filtering.
Limit
Type: int
Number of objects to return. Default/Max: 100.

Sample code


Response

{
"ChannelMembers": [
{
"UuidMetadata": {
"Uuid": "uuid-1",
"Name": "John Doe",
"Email": "jack@twitter.com",
"ExternalId": "",
"ProfileUrl": "",
"Custom": null,
"Updated": "2019-02-20T23:11:20.89375"
},
"Custom": {
"role": "admin"
},
show all 39 lines
Last updated on