App Context API for Java SDK
Breaking changes in v9.0.0
PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0) of the Java SDK.
For more details about what has changed, refer to Java/Kotlin SDK migration guide.
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 you need to build innovative, reliable, scalable applications. Use App Context to store metadata about your application users and channels, and their membership associations, without running your own databases.
PubNub also triggers events when object data changes: set, update, or removal. Setting the same data again doesn't trigger an event. Clients can receive these events in real time and update their front-end application accordingly.
User
Get metadata for all users
Returns a paginated list of UUID metadata. Optionally includes Custom.
Method(s)
Use the following in the Java SDK:
1pubnub.getAllUUIDMetadata()
2    .limit(Integer)
3    .page(PNPage)
4    .filter(String)
5    .sort(List<PNSortKey>)
6    .includeTotalCount(Boolean)
7    .includeCustom(Boolean)
| Parameter | Description | 
|---|---|
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String? Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeTotalCountType: Boolean Default: false | Whether to include the total count in the paginated response. Default is false. | 
| includeCustomType: Boolean Default: false | Whether to include the Custom object in the response. | 
Sample code
Reference code
1
Response
1public class PNGetAllUUIDMetadataResult extends EntityArrayEnvelope<PNUUIDMetadata> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNUUIDMetadata> data;
7    PNPage nextPage() {
8        return PNPage.next(next);
9    }
10    PNPage previousPage() {
11        return PNPage.previous(prev);
12    }
13}
14
15public class PNUUIDMetadata extends PNObject {
Get user metadata
Returns metadata for the specified UUID, optionally including the custom data object for each.
Method(s)
Use the following in the Java SDK:
1pubnub.getUUIDMetadata()
2    .uuid(String)
3    .includeCustom(Boolean)
| Parameter | Description | 
|---|---|
| uuidType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| includeCustomType: Boolean Default: false | Whether to include the Custom object in the response. | 
Sample code
1
Response
1public class PNGetUUIDMetadataResult extends EntityEnvelope<PNUUIDMetadata> {
2    int status;
3    PNUUIDMetadata data;
4}
5
6public class PNUUIDMetadata extends PNObject {
7    String id;
8    Object custom;
9    String updated;
10    String eTag;
11    String name;
12    String email;
13    String externalId;
14    String profileUrl;
15}
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)
Use the following in the Java SDK:
1pubnub.setUUIDMetadata()
2    .uuid(String)
3    .name(String)
4    .externalId(String)
5    .profileUrl(String)
6    .email(String)
7    .custom(Map<String, Object>)
8    .includeCustom(true)
9    .ifMatchesEtag(String)
| Parameter | Description | 
|---|---|
| uuidType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| nameType: String Default: n/a | Display name for the user. | 
| externalIdType: String Default: n/a | User's identifier in an external system. | 
| profileUrlType: String Default: n/a | The URL of the user's profile picture. | 
| emailType: String Default: n/a | The user's email address. | 
| customType: Any Default: n/a | Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported. | 
| includeCustomType: Boolean Default: false | Whether to include the customobject in the fetch response. | 
| ifMatchesEtagType: String Default: n/a | 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
1public class PNSetUUIDMetadataResult extends EntityEnvelope<PNUUIDMetadata> {
2    protected int status;
3    protected PNUUIDMetadata data;
4}
5
6public class PNUUIDMetadata extends PNObject {
7    String id;
8    Object custom;
9    String updated;
10    String eTag;
11    String name;
12    String email;
13    String externalId;
14    String profileUrl;
15}
Remove user metadata
Removes the metadata from a specified UUID.
Method(s)
Use the following in the Java SDK:
1pubnub.removeUUIDMetadata()
2    .uuid(String)
| Parameter | Description | 
|---|---|
| uuidType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
Sample code
1
Response
1public class PNRemoveUUIDMetadataResult extends EntityEnvelope<JsonElement> {
2    int status;
3    JsonElement data;
4}
Channel
Get metadata for all channels
Returns a paginated list of channel metadata. Optionally includes Custom.
Method(s)
Use the following in the Java SDK:
1pubnub.getAllChannelsMetadata(
2        .limit(Integer)
3        .page(PNPage)
4        .filter(String)
5        .sort(List<PNSortKey>)
6        .includeTotalCount(Boolean)
7        .includeCustom(Boolean)
| Parameter | Description | 
|---|---|
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeTotalCountType: Boolean Default: false | Whether to include the total count in the paginated response. Default is false. | 
| includeCustomType: Boolean Default: false | Whether to include the Custom object in the response. | 
Sample code
1
Response
1public class PNGetAllChannelsMetadataResult extends EntityArrayEnvelope<PNChannelMetadata> {
2    int status;
3    List<PNChannelMetadata> data;
4    Integer totalCount;
5    String next;
6    String prev;
7}
8
9public class PNChannelMetadata extends PNObject {
10    String id;
11    Object custom;
12    String updated;
13    String eTag;
14    String name;
15    String description;
Get channel metadata
Returns metadata for the specified Channel, optionally including the custom data object for each.
Method(s)
Use the following in the Java SDK:
1pubnub.getChannelMetadata()
2    .channel(String)
3    .includeCustom(Boolean)
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel name. | 
| includeCustomType: Boolean Default: false | Whether to include the Custom object in the response. | 
Sample code
1
Response
1public class PNGetChannelMetadataResult extends EntityEnvelope<PNChannelMetadata> {
2    protected int status;
3    protected PNChannelMetadata data;
4}
5
6public class PNChannelMetadata extends PNObject {
7    String id;
8    Object custom;
9    String updated;
10    String eTag;
11    String name;
12    String description;
13}
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:
- 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 Channel in the database, optionally including the custom data object for each.
Method(s)
Use the following in the Java SDK:
1pubnub.setChannelMetadata()
2    .channel(String)
3    .name(String)
4    .description(String)
5    .custom(Map<String, Object>)
6    .includeCustom(Boolean)
7    .ifMatchesEtag(String)
8
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel ID. | 
| nameType: String Default: n/a | Name for the channel. | 
| descriptionType: String Default: n/a | Description of a channel. | 
| customType: Map <String, Object>Default: n/a | Any object of key-value pairs with supported data types. App Context filtering language doesn’t support filtering by custom properties. | 
| includeCustomType: Boolean Default: false | Whether to include the customobject in the fetch response. | 
| ifMatchesEtagType: String Default: n/a | The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown. | 
API limits
To learn about the maximum length of parameters used to set channel metadata, refer to REST API docs.
Sample code
1
Response
1public class PNSetChannelMetadataResult extends EntityEnvelope<PNChannelMetadata> {
2    protected int status;
3    protected PNChannelMetadata data;
4}
5
6public class PNChannelMetadata extends PNObject {
7    String id;
8    Object custom;
9    String updated;
10    String eTag;
11    String name;
12    String description;
13}
Other examples
Iteratively update existing metadata
1
Remove channel metadata
Removes the metadata from a specified channel.
Method(s)
Use the following in the Java SDK:
1pubnub.removeChannelMetadata()
2    .channel(String)
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel ID. | 
Sample code
1
Response
1public class PNRemoveChannelMetadataResult extends EntityEnvelope<JsonElement> {
2    int status;
3    protected JsonElement data;
4}
Channel memberships
Get channel memberships
Returns a list of channel memberships for a user. Does not include subscriptions.
Method(s)
Use the following in the Java SDK:
1pubnub.getMemberships()
2    .userId(String)
3    .limit(Integer)
4    .page(PNPage)
5    .filter(String)
6    .sort(List<PNSortKey>)
7    .include(MembershipInclude)
8    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| userIdType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MembershipIncludeDefault: All parameters set to  false | Object holding the configuration for whether to include additional data in the response. | 
Sample code
1
Response
1public class PNGetMembershipsResult extends EntityArrayEnvelope<PNMembership> {
2    protected Integer totalCount;
3    protected String next;
4    protected String prev;
5    protected int status;
6    protected List<PNMembership> data;
7}
8
9public class PNMembership {
10    PNChannelMetadata channel;
11    Object custom;
12    String updated;
13    String eTag;
14}
Sample code with pagination
1
Set channel memberships
Set channel memberships for a User.
Method(s)
Use the following in the Java SDK:
1pubnub.setMemberships(Collection<PNChannelMembership>)
2    .userId(String)
3    .limit(Integer)
4    .page(PNPage)
5    .filter(String)
6    .sort(List<PNSortKey>)
7    .include(MembershipInclude)
8    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| channelMemberships*Type: List <PNChannelMembership>Default: n/a | Collection of PNChannelMembership to add to membership. | 
| userIdType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MembershipIncludeDefault: All parameters set to  false | Object holding the configuration for whether to include additional data in the response. | 
API limits
To learn about the maximum length of parameters used to set channel membership metadata, refer to REST API docs.
PNChannelMembership
PNChannelMembership is a utility class that uses the builder pattern to construct a channel membership with additional custom data.
| Parameter | Description | 
|---|---|
| channel*Type:  ChannelId | The name of the channel associated with this membership. | 
| customType:  Object | A dictionary that stores custom metadata related to the membership, allowing for additional context or information. | 
| statusType:  String | The status of the membership, for example: "active" or "inactive" | 
| typeType:  String | The type of membership for categorization purposes. | 
Sample code
1
Response
1public class PNSetMembershipResult extends EntityArrayEnvelope<PNMembership> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembership> data;
7}
8
9public class PNMembership {
10    PNChannelMetadata channel;
11    Object custom;
12    String updated;
13    String eTag;
14}
Remove channel memberships
Remove channel memberships for a User.
Method(s)
Use the following in the Java SDK:
1pubnub.removeMemberships(Collection<PNChannelMembership>)
2    .userId(String)
3    .limit(Integer)
4    .page(PNPage)
5    .filter(String)
6    .sort(List<PNSortKey>)
7    .include(MembershipInclude)
8    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| channelMemberships*Type: List <PNChannelMembership>Default: n/a | Collection of PNChannelMembership to add to membership. | 
| userIdType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MembershipIncludeDefault: All parameters set to  false | Object holding the configuration for whether to include additional data in the response. | 
Sample code
1
Response
1public class PNRemoveMembershipResults extends EntityArrayEnvelope<PNMembership> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembership> data;
7}
8
9public class PNMembership {
10    PNChannelMetadata channel;
11    Object custom;
12    String updated;
13    String eTag;
14}
Manage channel memberships
Manage a user's channel memberships.
Method(s)
Use the following in the Java SDK:
1pubnub.manageMemberships(Collection<PNChannelMembership>, Collection<String>)
2    .userId(String)
3    .limit(Integer)
4    .page(PNPage)
5    .filter(String)
6    .sort(List<PNSortKey>)
7    .include(MembershipInclude)
8    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| set*Type:  Collection<PNChannelMembership>Default: n/a | List of members PNChannelMembership to add to channel. | 
| remove*Type:  Collection<Stirng>Default: n/a | List of members channelIds to remove from channel. | 
| userIdType: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MembershipIncludeDefault: All parameters set to  false | Object holding the configuration for whether to include additional data in the response. | 
Sample code
1
Response
1public class PNManageMembershipResult extends EntityArrayEnvelope<PNMembership> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembership> data;
7}
8
9public class PNMembership {
10    PNChannelMetadata channel;
11    Object custom;
12    String updated;
13    String eTag;
14}
Channel members
Get channel members
Returns a list of channel members. Includes user metadata when available.
Method(s)
Use the following in the Java SDK:
1pubnub.getChannelMembers(String)
2    .limit(Integer)
3    .page(PNPage)
4    .filter(String)
5    .sort(List<PNSortKey>)
6    .include(MemberInclude)
7    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel ID. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MemberIncludeDefault: All parameters set to  false. | Object holding the configuration for whether to include additional data in the response. | 
Sample code
1
Response
1public class PNRemoveMembershipResults extends EntityArrayEnvelope<PNMembers> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembers> data;
7}
8
9public class PNMembers {
10    PNUUIDMetadata uuid;
11    Object custom;
12    String updated;
13    String eTag;
14}
Set channel members
This method sets members in a channel.
Method(s)
Use the following in the Java SDK:
1pubnub.setChannelMembers(String, Collection<PNUser>)
2    .limit(Integer)
3    .page(PNPage)
4    .filter(String)
5    .sort(List<PNSortKey>)
6    .include(MemberInclude)
7    .async(result -> { /* check result */ });
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel name. | 
| channelMembers*Type:  Collection<PNUser>Default: n/a | List of members to add to channel. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | Cursor-based pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | Sort by ID,NAME,UPDATED,STATUS,TYPEwith asc/desc for sort direction (for example,PNSortKey.asc(PNSortKey.Key.TYPE)). | 
| includeType:  MemberIncludeDefault: All parameters set to  false. | Object holding the configuration for whether to include additional data in the response. | 
API limits
To learn about the maximum length of parameters used to set channel members metadata, refer to REST API docs.
PNUser
PNUser is a utility class that utilizes the builder pattern to facilitate the construction of a user object with additional customization options. This class allows users to define custom metadata, a status, and a type for a user.
| Property | Description | 
|---|---|
| userId*Type:  String | The unique identifier for the user. This field cannot be null or empty. | 
| customType:  Object | A dictionary-like object that stores custom metadata related to the user, which provides additional context or information. | 
| statusType:  String | The status of the user, which can be any string such as activeorinactive. | 
| typeType:  String | The categorization type of the user, allowing for differentiation between user types. | 
Sample code
1
Response
1public class PNSetChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembers> data;
7}
8
9public class PNMembers {
10    PNUUIDMetadata uuid;
11    Object custom;
12    String updated;
13    String eTag;
14}
Remove channel members
Remove members from a Channel.
Method(s)
Use the following in the Java SDK:
1pubnub.removeChannelMembers(String, List<String>)
2    .limit(Integer)
3    .page(PNPage)
4    .filter(String)
5    .sort(List<PNSortKey>)
6    .includeTotalCount(Boolean)
7    .includeCustom(Boolean)
8    .includeUUID(PNUUIDDetailsLevel)
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel name. | 
| channelMembers*Type: Collection <String>Default: n/a | List of member userIds to remove from channel. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | The paging object used for pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | List of properties to sort by. Available options are PNSortKey.Key.ID,PNSortKey.Key.NAME,PNSortKey.Key.UPDATED,PNSortKey.Key.STATUSandPNSortKey.Key.TYPE. UsePNSortKey.ascorPNSortKey.descto specify sort direction. For example:PNSortKey.asc(PNSortKey.Key.TYPE)orPNSortKey.asc(PNSortKey.Key.STATUS) | 
| includeType:  MemberIncludeDefault: All parameters set to  false | Object defining options to include additional data in the response. | 
Sample code
1
Response
1public class PNRemoveChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembers> data;
7}
8
9public class PNMembers {
10    PNUUIDMetadata uuid;
11    Object custom;
12    String updated;
13    String eTag;
14}
Manage channel members
The method Set and Remove channel memberships for a user.
Method(s)
Use the following in the Java SDK:
1pubnub.manageChannelMembers(String, Collection<PNUser>, Collection<String>)
2    .limit(Integer)
3    .page(PNPage)
4    .filter(String)
5    .sort(List<PNSortKey>)
6    .include(MemberInclude)
| Parameter | Description | 
|---|---|
| channel*Type: String Default: n/a | Channel name. | 
| set*Type: Collection <PNUser>Default: n/a | List of members to add to channel. | 
| remove*Type: Collection <String>Default: n/a | List of userIds to remove from channel. | 
| limitType: Integer Default: 100 | Number of objects to return. Default/Max: 100. | 
| pageType: PNPage Default: n/a | The paging object used for pagination. | 
| filterType: String Default: n/a | Filter expression. Only matching objects are returned. See filtering. | 
| sortType: List <PNSortKey>Default: listOf() | List of properties to sort by. Available options are PNSortKey.Key.ID,PNSortKey.Key.NAME,PNSortKey.Key.UPDATED,PNSortKey.Key.STATUSandPNSortKey.Key.TYPE. UsePNSortKey.ascorPNSortKey.descto specify sort direction. For example:PNSortKey.asc(PNSortKey.Key.TYPE)orPNSortKey.asc(PNSortKey.Key.STATUS). | 
| includeType:  MemberIncludeDefault: All parameters set to  false | Object holding the configuration for whether to include additional data in the response. | 
Sample code
1
Response
1public class PNManageChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
2    Integer totalCount;
3    String next;
4    String prev;
5    int status;
6    List<PNMembers> data;
7}
8
9public class PNMembers {
10    PNUUIDMetadata uuid;
11    Object custom;
12    String updated;
13    String eTag;
14}