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.
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}
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:
1pubnub.GetAllUuidMetadata()
2 .IncludeCustom(bool)
3 .IncludeCount(bool)
4 .Page(PNPageObject)
5 .Sort(List<string>)
6 .Filter(string)
7 .Limit(int)
| Parameter | Description |
|---|---|
IncludeCustomType: bool | Whether to include the Custom object in the response. |
IncludeCountType: bool | Whether to include the total count in the paginated response. Default is false. |
PageType: PNPageObject | Cursor-based pagination. |
SortType: List <string> | Sort by id, name, updated with asc/desc (for example, {name: 'asc'}). |
FilterType: string | Filter expression. Only matching objects are returned. See filtering. |
LimitType: int | Number of objects to return. Default/Max: 100. |
Sample code
Reference code
1
Response
1{
2 "Uuids": [
3 {
4 "Uuid": "uuid-1",
5 "Name": "John Doe",
6 "Email": "jack@twitter.com",
7 "ExternalId": null,
8 "ProfileUrl": null,
9 "Custom": null,
10 "Updated": "2020-06-17T16:28:14.060718Z"
11 },
12 {
13 "Uuid": "uuid-2",
14 "Name": "Bob Cat",
15 "Email": "bobc@example.com",
show all 29 linesGet 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:
1pubnub.GetUuidMetadata()
2 .Uuid(string)
3 .IncludeCustom(bool)
| Parameter | Description |
|---|---|
Uuid *Type: string | Unique user identifier (UUID). If not supplied, the current user's UUID is used. |
IncludeCustomType: bool | Whether to include the Custom object in the response. |
Sample code
1
Response
1{
2 "Uuid": "uuid-1",
3 "Name": "John Doe",
4 "Email": "jack@twitter.com",
5 "ExternalId": null,
6 "ProfileUrl": null,
7 "Custom": null,
8 "Updated": "2020-06-17T16:28:14.060718Z"
9}
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:
- Get the existing metadata and store it locally.
- Append the new custom metadata to the existing one.
- 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:
1pubnub.SetUuidMetadata()
2 .Uuid(string)
3 .Name(string)
4 .Email(string)
5 .ExternalId(string)
6 .ProfileUrl(string)
7 .Custom(Dictionary<string, object>)
8 .IncludeCustom(bool)
9 .IfMatchesEtag(string)
| Parameter | Description |
|---|---|
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. |
EmailType: string | The user's email address. |
ExternalIdType: string | User's identifier in an external system. |
ProfileUrlType: string | The URL of the user's profile picture. |
CustomType: Dictionary <string, object> | Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported. |
IncludeCustomType: bool | Whether to include the Custom object in the response. |
IfMatchesEtagType: 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
1
Response
1{
2 "Uuid": "uuid-1",
3 "Name": "John Doe",
4 "Email": "jack@twitter.com",
5 "ExternalId": null,
6 "ProfileUrl": null,
7 "Custom": null,
8 "Updated": "2020-06-17T16:28:14.060718Z"
9}
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:
1pubnub.RemoveUuidMetadata()
2 .Uuid(string)
| Parameter | Description |
|---|---|
Uuid *Type: string | Unique user identifier (UUID). If not supplied, the current user's UUID is used. |
Sample code
1
Response
1{}