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 easily store metadata about your application users and channels, and their membership associations, without the need to stand up your own databases.
PubNub also triggers events when object data is changed: set, updated, or removed from the database. At the same time, making a request to set the same data that already exist, doesn't trigger any 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 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 Java SDK:
pubnub.getAllUUIDMetadata()
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.includeTotalCount(Boolean)
.includeCustom(Boolean)
Parameter | Description |
---|---|
limit Type: Integer Default: 100 | The maximum number of objects to retrieve at a time. |
page Type: PNPage Default: n/a | The paging object used for pagination. |
filter Type: String? Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here |
sort Type: List <PNSortKey> Default: listOf() | List of properties to sort by. Available options are PNSortKey.Key.ID , PNSortKey.Key.NAME , PNSortKey.Key.UPDATED , PNSortKey.Key.STATUS and PNSortKey.Key.TYPE . Use PNSortKey.asc or PNSortKey.desc to specify sort direction. For example: PNSortKey.asc(PNSortKey.Key.TYPE) or PNSortKey.asc(PNSortKey.Key.STATUS) . |
includeTotalCount Type: Boolean Default: false | Request totalCount to be included in paginated response, which is omitted by default. |
includeCustom Type: Boolean Default: false | Whether to include the custom object in the fetch response. |
Basic Usage
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.
import com.pubnub.api.PubNubException;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.endpoints.objects_api.utils.PNSortKey;
import com.pubnub.api.java.models.consumer.objects_api.uuid.PNUUIDMetadata;
import com.pubnub.api.java.v2.PNConfiguration;
import com.pubnub.api.UserId;
import com.pubnub.api.enums.PNLogVerbosity;
import java.util.Arrays;
import java.util.List;
public class GetAllUUIDMetadataApp {
public static void main(String[] args) throws PubNubException {
// Configure PubNub instance
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
configBuilder.publishKey("demo");
show all 42 linesResponse
public class PNGetAllUUIDMetadataResult extends EntityArrayEnvelope<PNUUIDMetadata> {
Integer totalCount;
String next;
String prev;
int status;
List<PNUUIDMetadata> data;
PNPage nextPage() {
return PNPage.next(next);
}
PNPage previousPage() {
return PNPage.previous(prev);
}
}
public class PNUUIDMetadata extends PNObject {
show all 24 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 Java SDK:
pubnub.getUUIDMetadata()
.uuid(String)
.includeCustom(Boolean)
Parameter | Description |
---|---|
uuid Type: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. |
includeCustom Type: Boolean Default: false | Whether to include the custom object in the fetch response. |
Basic Usage
pubnub.getUUIDMetadata().async(result -> { /* check result */ });
Response
public class PNGetUUIDMetadataResult extends EntityEnvelope<PNUUIDMetadata> {
int status;
PNUUIDMetadata data;
}
public class PNUUIDMetadata extends PNObject {
String id;
Object custom;
String updated;
String eTag;
String name;
String email;
String externalId;
String profileUrl;
}
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 Java SDK:
pubnub.setUUIDMetadata()
.uuid(String)
.name(String)
.externalId(String)
.profileUrl(String)
.email(String)
.custom(Map<String, Object>)
.includeCustom(true)
.ifMatchesEtag(String)
Parameter | Description |
---|---|
uuid Type: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. |
name Type: String Default: n/a | Display name for the user. |
externalId Type: String Default: n/a | User's identifier in an external system. |
profileUrl Type: String Default: n/a | The URL of the user's profile picture. |
email Type: String Default: n/a | The user's email address. |
custom Type: Any Default: n/a | Any object of key-value pairs with supported data types. App Context filtering language doesn’t support filtering by custom properties. |
includeCustom Type: Boolean Default: false | Whether to include the custom object in the fetch response. |
ifMatchesEtag Type: 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 user metadata, refer to REST API docs.
Basic Usage
pubnub.setUUIDMetadata()
.name("Foo")
.profileUrl("http://example.com")
.email("foo@example.com")
.includeCustom(true)
.async(result -> { /* check result */ });
Response
public class PNSetUUIDMetadataResult extends EntityEnvelope<PNUUIDMetadata> {
protected int status;
protected PNUUIDMetadata data;
}
public class PNUUIDMetadata extends PNObject {
String id;
Object custom;
String updated;
String eTag;
String name;
String email;
String externalId;
String profileUrl;
}
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 Java SDK:
pubnub.removeUUIDMetadata()
.uuid(String)
Parameter | Description |
---|---|
uuid Type: String Default: pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. |
Basic Usage
pubnub.removeUUIDMetadata()
.async(result -> { /* check result */ });
Response
public class PNRemoveUUIDMetadataResult extends EntityEnvelope<JsonElement> {
int status;
JsonElement data;
}
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 Java SDK:
pubnub.getAllChannelsMetadata(
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.includeTotalCount(Boolean)
.includeCustom(Boolean)
Parameter | Description |
---|---|
limit Type: Integer Default: 100 | The maximum number of objects to retrieve at a time. |
page Type: PNPage Default: n/a | The paging object used for pagination. |
filter Type: String Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here |
sort Type: List <PNSortKey> Default: listOf() | List of properties to sort by. Available options are PNSortKey.Key.ID , PNSortKey.Key.NAME , PNSortKey.Key.UPDATED , PNSortKey.Key.STATUS and PNSortKey.Key.TYPE . Use PNSortKey.asc or PNSortKey.desc to specify sort direction. For example: PNSortKey.asc(PNSortKey.Key.TYPE) or PNSortKey.asc(PNSortKey.Key.STATUS) . |
includeTotalCount Type: Boolean Default: false | Request totalCount to be included in paginated response, which is omitted by default. |
includeCustom Type: Boolean Default: false | Whether to include the custom object in the fetch response. |
Basic Usage
pubnub.getAllChannelsMetadata()
.async(result -> { /* check result */ });
Response
public class PNGetAllChannelsMetadataResult extends EntityArrayEnvelope<PNChannelMetadata> {
int status;
List<PNChannelMetadata> data;
Integer totalCount;
String next;
String prev;
}
public class PNChannelMetadata extends PNObject {
String id;
Object custom;
String updated;
String eTag;
String name;
String description;
show all 16 linesGet 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 Java SDK:
pubnub.getChannelMetadata()
.channel(String)
.includeCustom(Boolean)
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Channel name. |
includeCustom Type: Boolean Default: false | Whether to include the custom object in the fetch response. |