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)
* required
ParameterDescription
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

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 lines

Response

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 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 Java SDK:

pubnub.getUUIDMetadata()
.uuid(String)
.includeCustom(Boolean)
* required
ParameterDescription
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:

  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 Java SDK:

pubnub.setUUIDMetadata()
.uuid(String)
.name(String)
.externalId(String)
.profileUrl(String)
.email(String)
.custom(Map<String, Object>)
.includeCustom(true)
.ifMatchesEtag(String)
* required
ParameterDescription
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)
* required
ParameterDescription
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)
* required
ParameterDescription
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 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 Java SDK:

pubnub.getChannelMetadata()
.channel(String)
.includeCustom(Boolean)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
includeCustom
Type: Boolean
Default:
false
Whether to include the custom object in the fetch response.

Basic Usage

pubnub.getChannelMetadata()
.channel("myChannel")
.async(result -> { /* check result */ });

Response

public class PNGetChannelMetadataResult extends EntityEnvelope<PNChannelMetadata> {
protected int status;
protected PNChannelMetadata data;
}

public class PNChannelMetadata extends PNObject {
String id;
Object custom;
String updated;
String eTag;
String name;
String description;
}

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 Java SDK:

pubnub.setChannelMetadata()
.channel(String)
.name(String)
.description(String)
.custom(Map<String, Object>)
.includeCustom(Boolean)
.ifMatchesEtag(String)

* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
name
Type: String
Default:
n/a
Name for the channel.
description
Type: String
Default:
n/a
Description of a channel.
custom
Type: 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.
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 channel metadata, refer to REST API docs.

Basic Usage

pubnub.setChannelMetadata()
.channel("myChannel")
.name("Some Name")
.includeCustom(true)
.async(result -> { /* check result */ });

Response

public class PNSetChannelMetadataResult extends EntityEnvelope<PNChannelMetadata> {
protected int status;
protected PNChannelMetadata data;
}

public class PNChannelMetadata extends PNObject {
String id;
Object custom;
String updated;
String eTag;
String name;
String description;
}

Other Examples

Iteratively update existing metadata
import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.models.consumer.objects_api.channel.PNChannelMetadata;
import com.pubnub.api.java.models.consumer.objects_api.channel.PNSetChannelMetadataResult;
import com.pubnub.api.java.v2.PNConfiguration;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class UpdateCustomChannelMetadata {
public static void main(String[] args) throws PubNubException{
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("user01"), "demo").publishKey("demo");
show all 69 lines

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 Java SDK:

pubnub.removeChannelMetadata()
.channel(String)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.

Basic Usage

pubnub.removeChannelMetadata()
.channel("myChannel")
.async(result -> { /* check result */ });

Response

public class PNRemoveChannelMetadataResult extends EntityEnvelope<JsonElement> {
int status;
protected JsonElement data;
}

Channel Memberships

Get Channel Memberships

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

Method(s)

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

pubnub.getMemberships()
.userId(String)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MembershipInclude)
.async(result -> { /* check result */ });

| Parameter | Type | Required | Default | | :-------- | :-------- | :-------- |:--------|:-----------------| | userId | String | Optional | pubnub.getConfiguration().getUserId().getValue() | Unique User Metadata identifier. If not supplied, then userId from configuration will be used. | | limit | Integer | Optional | 100| The maximum number of objects to retrieve at a time. | | page | PNPage | Optional | | The paging object used for pagination. | | filter | String | Optional | | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here | | sort | List<PNSortKey> | Optional | 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). | | include | MembershipInclude | Optional | All parameters set to false | Object holding the configuration for whether to include additional data in the response. |

Basic Usage

pubnub.getMemberships()
.userId("userId01")
.limit(10)
.include(MembershipInclude.builder()
.includeCustom(true)
.includeStatus(true)
.includeType(true)
.includeTotalCount(true)
.includeChannel(true)
.includeChannelCustom(true)
.includeChannelType(true)
.includeChannelStatus(true)
.build())
.sort(Arrays.asList(PNSortKey.asc(PNSortKey.Key.TYPE)))
.async(result -> { /* check result */ });

Response

public class PNGetMembershipsResult extends EntityArrayEnvelope<PNMembership> {
protected Integer totalCount;
protected String next;
protected String prev;
protected int status;
protected List<PNMembership> data;
}

public class PNMembership {
PNChannelMetadata channel;
Object custom;
String updated;
String eTag;
}

Basic Usage with Pagination

final PNGetMembershipsResult getMembershipsResult = pubnub.getMemberships()
.limit(3)
.include(MembershipInclude.builder()
.includeCustom(true)
.includeStatus(true)
.includeType(true)
.includeTotalCount(true)
.includeChannel(true)
.build())
.sync();
if (getMembershipsResult.getNext() != null) {
final PNGetMembershipsResult getMembershipsNextPageResult = pubnub.getMemberships()
.page(getMembershipsResult.nextPage())
.limit(3)
.include(MembershipInclude.builder()
show all 24 lines

Set Channel Memberships

Set channel memberships for a User.

Method(s)

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

pubnub.setMemberships(Collection<PNChannelMembership>)
.userId(String)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MembershipInclude)
.async(result -> { /* check result */ });
* required
ParameterDescription
channelMemberships *
Type: List<PNChannelMembership>
Default:
n/a
Collection of PNChannelMembership to add to membership.
userId
Type: String
Default:
pubnub.getConfiguration().getUserId().getValue()
Unique User Metadata identifier. If not supplied, then userId from configuration will be used.
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).
include
Type: MembershipInclude
Default:
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.

* required
ParameterDescription
channel *
Type: ChannelId
The name of the channel associated with this membership.
custom
Type: Object
A dictionary that stores custom metadata related to the membership, allowing for additional context or information.
status
Type: String
The status of the membership, for example: "active" or "inactive"
type
Type: String
The type of membership for categorization purposes.
PNChannelMembership membership =  PNChannelMembership.builder(membership02ChannelId)
.custom(customChannelMembershipObject())
.status(membership02Status)
.type(membership02Type)
.build();

Basic Usage

pubnub.setMemberships(channelMemberships)
.include(MembershipInclude.builder()
.includeTotalCount(true)
.includeCustom(true)
.includeChannel(true)
.includeChannelCustom(true)
.build())
.async(result -> { /* check result */ });

Response

public class PNSetMembershipResult extends EntityArrayEnvelope<PNMembership> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembership> data;
}

public class PNMembership {
PNChannelMetadata channel;
Object custom;
String updated;
String eTag;
}

Remove Channel Memberships

Remove channel memberships for a User.

Method(s)

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

pubnub.removeMemberships(Collection<PNChannelMembership>)
.userId(String)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MembershipInclude)
.async(result -> { /* check result */ });
* required
ParameterDescription
channelMemberships *
Type: List<PNChannelMembership>
Default:
n/a
Collection of PNChannelMembership to add to membership.
userId
Type: String
Default:
pubnub.getConfiguration().getUserId().getValue()
Unique User Metadata identifier. If not supplied, then userId from configuration will be used.
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).
include
Type: MembershipInclude
Default:
All parameters set to false
Object holding the configuration for whether to include additional data in the response.

Basic Usage

pubnub.removeMemberships(channelMembershipsToRemove)
.include(MembershipInclude.builder()
.includeTotalCount(true)
.includeCustom(true)
.includeChannel(true)
.includeType(true)
.includeStatus(true)
.build())
.async(result -> { /* check result */ });

Response

public class PNRemoveMembershipResults extends EntityArrayEnvelope<PNMembership> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembership> data;
}

public class PNMembership {
PNChannelMetadata channel;
Object custom;
String updated;
String eTag;
}

Manage Channel Memberships

Manage a user's channel memberships.

Method(s)

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

pubnub.manageMemberships(Collection<PNChannelMembership>, Collection<String>)
.userId(String)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MembershipInclude)
.async(result -> { /* check result */ });
* required
ParameterDescription
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.
userId
Type: String
Default:
pubnub.getConfiguration().getUserId().getValue()
Unique User Metadata identifier. If not supplied, then userId from configuration will be used.
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)
include
Type: MembershipInclude
Default:
All parameters set to false
Object holding the configuration for whether to include additional data in the response.

Basic Usage

final List<PNChannelMembership> channelMembershipsToSet = Collections.singletonList(
PNChannelMembership.builder("channelId02")
.custom(customChannelMembershipObject())
.status(membership02Status)
.type(membership02Type)
.build());
final List<String> channelIdsToRemove = Collections.singletonList("channelId01");

pubnub.manageMemberships(channelMembershipsToSet, channelIdsToRemove)
.userId("userId01")
.include(MembershipInclude.builder()
.includeTotalCount(true)
.includeCustom(true)
.includeChannel(true)
.includeChannelCustom(true)
show all 19 lines

Response

public class PNManageMembershipResult extends EntityArrayEnvelope<PNMembership> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembership> data;
}

public class PNMembership {
PNChannelMetadata channel;
Object custom;
String updated;
String eTag;
}

Channel Members

Get Channel Members

The method returns a list of members in a channel. The list will include 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 Java SDK:

pubnub.getChannelMembers(String)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MemberInclude)
.async(result -> { /* check result */ });
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
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).
include
Type: MemberInclude
Default:
All parameters set to false.
Object holding the configuration for whether to include additional data in the response.

Basic Usage

pubnub.getChannelMembers(testChannelId)
.include(MemberInclude.builder()
.includeTotalCount(true)
.includeStatus(true)
.includeType(true)
.includeCustom(true)
.includeUser(true)
.includeUserCustom(true)
.build())
.sort(Arrays.asList(PNSortKey.desc(PNSortKey.Key.STATUS)))
.async(result -> { /* check result */ });

Response

public class PNRemoveMembershipResults extends EntityArrayEnvelope<PNMembers> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembers> data;
}

public class PNMembers {
PNUUIDMetadata uuid;
Object custom;
String updated;
String eTag;
}

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 Java SDK:

pubnub.setChannelMembers(String, Collection<PNUser>)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MemberInclude)
.async(result -> { /* check result */ });
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
channelMembers *
Default:
n/a
List of members to add to channel.
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)
include
Type: MemberInclude
Default:
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.

PropertyTypeRequiredDescription
userId
String
Yes
The unique identifier for the user. This field cannot be null or empty.
custom
Object
No
A dictionary-like object that stores custom metadata related to the user, which provides additional context or information.
status
String
No
The status of the user, which can be any string such as active or inactive.
type
String
No
The categorization type of the user, allowing for differentiation between user types.
PNUser user1 = PNUser.builder(TEST_UUID1)
.custom(customChannelMembershipObject()) // Sets custom metadata
.status(STATUS_01) // Sets the user status
.type(TYPE_01) // Sets the user type
.build(); // Constructs the PNUser instance

Basic Usage

pubnub.setChannelMembers(testChannelId, channelMembers)
.include(MemberInclude.builder()
.includeCustom(true)
.includeStatus(true)
.includeType(true)
.includeTotalCount(true)
.includeUser(true)
.includeUserCustom(true)
.includeUserStatus(true)
.includeUserType(true)
.build())
.sort(Arrays.asList(PNSortKey.asc(PNSortKey.Key.STATUS)))
.async(result -> { /* check result */ });

Response

public class PNSetChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembers> data;
}

public class PNMembers {
PNUUIDMetadata uuid;
Object custom;
String updated;
String eTag;
}

Remove Channel Members

Remove members from a Channel.

Method(s)

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

pubnub.removeChannelMembers(String, List<String>)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.includeTotalCount(Boolean)
.includeCustom(Boolean)
.includeUUID(PNUUIDDetailsLevel)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
channelMembers *
Type: Collection<String>
Default:
n/a
List of member userIds to remove from channel.
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)
include
Type: MemberInclude
Default:
All parameters set to false
Object defining options to include additional data in the response.

Basic Usage

pubnub.removeChannelMembers("channelId", Collections.singletonList("userId"))
.include(MemberInclude.builder()
.includeTotalCount(true)
.includeStatus(true)
.includeType(true)
.includeCustom(true)
.includeUser(true)
.includeUserCustom(true)
.build())
.async(result -> { /* check result */ });

Response

public class PNRemoveChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembers> data;
}

public class PNMembers {
PNUUIDMetadata uuid;
Object custom;
String updated;
String eTag;
}

Manage Channel Members

The method Set and Remove channel memberships for a user.

Method(s)

To Manage Channel Members you can use the following method(s) in the Java SDK:

pubnub.manageChannelMembers(String, Collection<PNUser>, Collection<String>)
.limit(Integer)
.page(PNPage)
.filter(String)
.sort(List<PNSortKey>)
.include(MemberInclude)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
set *
Default:
n/a
List of members to add to channel.
remove *
Type: Collection<String>
Default:
n/a
List of userIds to remove from channel.
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).
include
Type: MemberInclude
Default:
All parameters set to false
Object holding the configuration for whether to include additional data in the response.

Basic Usage

final List<PNUser> channelMembersToSet = Collections.singletonList(
PNUser.builder("userId02").custom(customChannelMembershipObject()).status("status02").type("type02").build());
final List<String> channelMembersIdsToRemove = Collections.singletonList("userId01");

pubnub.manageChannelMembers("channelId", channelMembersToSet, channelMembersIdsToRemove)
.include(MemberInclude.builder()
.includeCustom(true)
.includeStatus(true)
.includeType(true)
.includeTotalCount(true)
.includeUser(true)
.includeUserCustom(true)
.includeUserStatus(true)
.includeUserType(true)
.build())
show all 16 lines

Response

public class PNManageChannelMembersResult extends EntityArrayEnvelope<PNMembers> {
Integer totalCount;
String next;
String prev;
int status;
List<PNMembers> data;
}

public class PNMembers {
PNUUIDMetadata uuid;
Object custom;
String updated;
String eTag;
}
Last updated on