App Context API for Kotlin SDK

Starting a new app? Use DataSync

DataSync is the successor to App Context. It covers the same users, channels, and memberships, and adds typed schemas, JSON Patch updates with ETags, field-level projections, and per-class expiry. Refer to the feature-by-feature comparison for details.

DataSync is currently available to new accounts and to accounts that are not actively using App Context. If your keysets already use App Context, keep using it for now. App Context remains fully supported.

Breaking changes in v9.0.0

PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java 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 Kotlin 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.

Request execution

Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously.

You must invoke the .sync() or .async() method on the Endpoint to execute the request, or the operation will not be performed.

1val channel = pubnub.channel("channelName")
2
3channel.publish("This SDK rules!").async { result ->
4 result.onFailure { exception ->
5 // Handle error
6 }.onSuccess { value ->
7 // Handle successful method result
8 }
9}

User​

Get metadata for all users​

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

Required keyset configuration
To get all channel and user metadata, you must uncheck the Disallow Get All Channel Metadata and Disallow Get All User Metadata checkboxes in the App Context section of your keyset configuration in the Admin Portal.

Method(s)​

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

1pubnub.getAllUUIDMetadata(
2 filter: String? = null,
3 sort: Collection<PNSortKey<PNKey>> = listOf(),
4 page: PNPage? = null,
5 limit: Int? = null,
6 includeCustom: Boolean = false,
7 includeCount: Boolean = false
8).async { result -> }
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNKey>>
Default: listOf()
Sort by ID, NAME, UPDATED, TYPE, STATUS with asc/desc for sort direction (for example, PNSortKey.asc(PNKey.STATUS)).
page#
Type: PNPage?
Default: null
Cursor-based pagination.
limit#
Type: Int
Default: 100
Number of objects to return. Default/Max: 100.
includeCustom#
Type: Boolean
Default: false
Whether to include the Custom object in the response.
includeCount#
Type: Boolean
Default: false
Whether to include the total count in the paginated response. Default is false.

Sample code​

1

2import com.pubnub.api.PubNub
3import com.pubnub.api.UserId
4import com.pubnub.api.v2.PNConfiguration
5
6/**
7 * This example demonstrates how to use the getAllUUIDMetadata method in the PubNub Kotlin SDK.
8 *
9 * The App Context (Objects) API allows you to store metadata about users (UUIDs) in PubNub's
10 * database without setting up your own backend.
11 */
12fun main() {
13 println("PubNub UUID Metadata Example")
14 println("============================")
15
show all 212 lines

Response​

1data class PNUUIDMetadataArrayResult(
2 val status: Int,
3 val data: Collection<PNUUIDMetadata>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNUUIDMetadata(
10 val id: String,
11 val name: PatchValue<String?>? = null,
12 val externalId: PatchValue<String?>? = null,
13 val profileUrl: PatchValue<String?>? = null,
14 val email: PatchValue<String?>? = null,
15 val custom: PatchValue<Map<String, Any?>?>? = null,
show all 20 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 Kotlin SDK:

1pubnub.getUUIDMetadata(
2 uuid: String? = null,
3 includeCustom: Boolean = false
4).async { result -> }
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
includeCustom#
Type: Boolean
Default: false
Whether to include the Custom object in the response.

Sample code​

1pubnub.getUUIDMetadata()
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNUUIDMetadataResult(
2 val status: Int,
3 val data: PNUUIDMetadata?
4)
5
6data class PNUUIDMetadata(
7 val id: String,
8 val name: PatchValue<String?>? = null,
9 val externalId: PatchValue<String?>? = null,
10 val profileUrl: PatchValue<String?>? = null,
11 val email: PatchValue<String?>? = null,
12 val custom: PatchValue<Map<String, Any?>?>? = null,
13 val updated: PatchValue<String>? = null,
14 val eTag: PatchValue<String>? = null,
15 val type: PatchValue<String?>? = null,
show all 17 lines

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

1pubnub.setUUIDMetadata(
2 uuid: String? = null,
3 includeCustom: Boolean = false,
4 name: String? = null,
5 externalId: String? = null,
6 profileUrl: String? = null,
7 email: String? = null,
8 custom: Any? = null,
9 type: String?,
10 status: String?,
11 ifMatchesEtag: String?
12).async { result -> }
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
includeCustom#
Type: Boolean
Default: false
Whether to include the custom field in the fetch response.
name#
Type: String?
Default: null
Display name for the user.
externalId#
Type: String?
Default: null
User's identifier in an external system
profileUrl#
Type: String?
Default: null
The URL of the user's profile picture
email#
Type: String?
Default: null
The user's email address.
type#
Type: String?
Default: null
The custom type of the user.
status#
Type: String?
Default: null
The custom type of the user.
custom#
Type: Any?
Default: null
Any object of key-value pairs with supported data types. App Context filtering language doesn’t support filtering by custom properties.
ifMatchesEtag#
Type: String
Default: no default
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​

1pubnub.setUUIDMetadata()
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNUUIDMetadataResult(
2 val status: Int,
3 val data: PNUUIDMetadata?
4)
5
6data class PNUUIDMetadata(
7 val id: String,
8 val name: PatchValue<String?>? = null,
9 val externalId: PatchValue<String?>? = null,
10 val profileUrl: PatchValue<String?>? = null,
11 val email: PatchValue<String?>? = null,
12 val custom: PatchValue<Map<String, Any?>?>? = null,
13 val updated: PatchValue<String>? = null,
14 val eTag: PatchValue<String>? = null,
15 val type: PatchValue<String?>? = null,
show all 17 lines

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

1pubnub.removeUUIDMetadata(
2 uuid: String? = null
3).async { result -> }
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.

Sample code​

1pubnub.removeUUIDMetadata()
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNRemoveMetadataResult(private val status: Int)

Channel​

Get metadata for all channels​

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

Required keyset configuration
To get all channel and user metadata, you must uncheck the Disallow Get All Channel Metadata and Disallow Get All User Metadata checkboxes in the App Context section of your keyset configuration in the Admin Portal.

Method(s)​

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

1pubnub.getAllChannelMetadata(
2 filter: String? = null,
3 sort: Collection<PNSortKey<PNKey>> = listOf(),
4 page: PNPage? = null,
5 limit: Int? = null,
6 includeCustom: Boolean = false,
7 includeCount: Boolean = false,
8).async { result -> }
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNKey>>
Default: listOf()
List of properties to sort by. Available options are PNKey.ID, PNKey.NAME, PNKey.UPDATED, PNKey.TYPE, PNKey.STATUS. Use PNSortKey.asc or PNSortKey.desc to specify sort direction. For example: PNSortKey.asc(PNKey.STATUS) , PNSortKey.desc(PNKey.TYPE)
page#
Type: PNPage?
Default: null
The paging object used for pagination.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
includeCustom#
Type: Boolean
Default: false
Whether to include the Custom field in the fetch response.
includeCount#
Type: Boolean
Default: false
Request IncludeCount to be included in paginated response. By default, IncludeCount is omitted.

Sample code​

1pubnub.getAllChannelMetadata()
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNChannelMetadataArrayResult(
2 val status: Int,
3 val data: Collection<PNChannelMetadata>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNChannelMetadata(
10 val id: String,
11 val name: PatchValue<String?>? = null,
12 val description: PatchValue<String?>? = null,
13 val custom: PatchValue<Map<String, Any?>?>? = null,
14 val updated: PatchValue<String>? = null,
15 val eTag: PatchValue<String>? = null,
show all 18 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 Kotlin SDK:

1pubnub.getChannelMetadata(
2 channel: String,
3 includeCustom: Boolean = false
4).async { result -> }
channel#
Type: String
Default: no default
Channel name.
includeCustom#
Type: Boolean
Default: false
Whether to include the custom field in the fetch response.

Sample code​

1pubnub.getChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNChannelMetadataResult(
2 val status: Int,
3 val data: PNChannelMetadata?
4)
5
6data class PNChannelMetadata(
7 val id: String,
8 val name: PatchValue<String?>? = null,
9 val description: PatchValue<String?>? = null,
10 val custom: PatchValue<Map<String, Any?>?>? = null,
11 val updated: PatchValue<String>? = null,
12 val eTag: PatchValue<String>? = null,
13 val type: PatchValue<String?>? = null,
14 val status: PatchValue<String?>? = null,
15)

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

1pubnub.setChannelMetadata(
2 channel: String,
3 includeCustom: Boolean = false,
4 name: String? = null,
5 description: String? = null,
6 custom: Any? = null
7 type: String?,
8 status: String?,
9 ifMatchesEtag: String?
10).async { result -> }
channel#
Type: String
Default: no default
Channel name.
includeCustom#
Type: Boolean
Default: false
Whether to include the custom field in the fetch response.
name#
Type: String?
Default: null
Name for the channel.
description#
Type: String?
Default: null
Description of a channel.
type#
Type: String?
Default: null
The custom type of the channel.
status#
Type: String?
Default: null
The custom type of the channel.
custom#
Type: Any?
Default: null
Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported.
ifMatchesEtag#
Type: String
Default: no default
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​

1pubnub.setChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNChannelMetadataResult(
2 val status: Int,
3 val data: PNChannelMetadata?
4)
5
6data class PNChannelMetadata(
7 val id: String,
8 val name: PatchValue<String?>? = null,
9 val description: PatchValue<String?>? = null,
10 val custom: PatchValue<Map<String, Any?>?>? = null,
11 val updated: PatchValue<String>? = null,
12 val eTag: PatchValue<String>? = null,
13 val type: PatchValue<String?>? = null,
14 val status: PatchValue<String?>? = null,
15)

Other examples​

Iteratively update existing metadata​
1import com.pubnub.api.PubNub
2import com.pubnub.api.UserId
3import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadataResult
4import com.pubnub.api.v2.PNConfiguration
5import kotlin.test.assertEquals
6
7fun main() {
8 // create configuration
9 val pnConfiguration = PNConfiguration.builder(userId = UserId("user01"), subscribeKey = "demo") {
10 publishKey = "demo"
11 }.build()
12
13 // create PubNub
14 val pubNub = PubNub.create(pnConfiguration)
15
show all 56 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 Kotlin SDK:

1pubnub.removeChannelMetadata(
2 channel: String
3).async { result -> }
channel#
Type: String
Default: no default
Channel name.

Sample code​

1pubnub.removeChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNRemoveMetadataResult(private val status: Int)

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 Channel Memberships you can use the following method(s) in the Kotlin SDK:

1pubnub.getMemberships(
2 userId: String? = null,
3 limit: Int? = null,
4 page: PNPage? = null,
5 filter: String? = null,
6 sort: Collection<PNSortKey<PNMembershipKey>> = listOf(),
7 include: MembershipInclude = MembershipInclude(),
8).async { result -> }
userId#
Type: String
Default: pubnub.configuration.userId.value
Unique User Metadata identifier.
If not supplied, then userId from configuration will be used.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMembershipKey>>
Default: listOf()
Sort by CHANNEL_ID, CHANNEL_NAME, CHANNEL_UPDATED, CHANNEL_STATUS, CHANNEL_TYPE, UPDATED, STATUS, TYPE with asc/desc for sort direction (for example, PNSortKey.PNAsc(PNMembershipKey.TYPE)).
include#
Type: MembershipInclude
Default: MembershipInclude()
Object holding the configuration for whether to include additional data in the response.

Sample code​

1pubnub.getMemberships(
2 include = MembershipInclude(
3 includeCustom = true,
4 includeStatus = true,
5 includeType = true,
6 includeTotalCount = true,
7 includeChannel = true,
8 includeChannelCustom = true,
9 includeChannelType = true,
10 includeChannelStatus = true
11 ),
12 sort = listOf(PNSortKey.PNAsc(PNMembershipKey.CHANNEL_ID))
13).async { result ->
14 result.onFailure { exception ->
15 // Handle error
show all 19 lines

Response​

1data class PNChannelMembershipArrayResult(
2 val status: Int,
3 val data: Collection<PNChannelMembership>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNChannelMembership(
10 val channel: PNChannelMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>? = null,
15 val type: PatchValue<String?>? = null
show all 16 lines

Set channel memberships​

Set channel memberships for a User.

Method(s)​

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

1pubnub.setMemberships(
2 channels: List<ChannelMembershipInput>,
3 userId: String? = null,
4 limit: Int? = null,
5 page: PNPage? = null,
6 filter: String? = null,
7 sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
8 include: MembershipInclude = MembershipInclude(),
9).async { result -> }
channels#
Default: no default
List of ChannelMembershipInput to add to membership with optional custom metadata, like status or type.
userId#
Type: String
Default: pubnub.configuration.userId.value
Unique User Metadata identifier.
If not supplied, then userId from configuration will be used.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMembershipKey>>
Default: listOf()
List of properties to sort by. Available options are PNMembershipKey.CHANNEL_ID, PNMembershipKey.CHANNEL_NAME, PNMembershipKey.CHANNEL_UPDATED, PNMembershipKey.CHANNEL_STATUS, PNMembershipKey.CHANNEL_TYPE, PNMembershipKey.UPDATED, PNMembershipKey.STATUS and PNMembershipKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMembershipKey.TYPE) or PNSortKey.PNDesc(PNMembershipKey.STATUS).
include#
Type: MembershipInclude
Default: MembershipInclude()
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.

ChannelMembershipInput​

channel#
Type: String
The channel to add the membership to.
custom#
Type: CustomObject
Additional information about the membership.
type#
Type: String
The string description of the type of the membership.
status#
Type: String
The string description of the status of the membership.

Sample code​

1pubnub.setMemberships(
2 channels = listOf(
3 PNChannelMembership.Partial(
4 channelId = "myChannel",
5 custom = mapOf("owner" to "PubNub"),
6 status = "active",
7 type = "regular_membership"
8 )
9 ),
10 include = MembershipInclude(
11 includeCustom = true,
12 includeStatus = true,
13 includeType = true,
14 includeChannel = true,
15 includeChannelCustom = true,
show all 26 lines

Response​

1data class PNChannelMembershipArrayResult(
2 val status: Int,
3 val data: Collection<PNChannelMembership>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNChannelMembership(
10 val channel: PNChannelMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>? = null,
15 val type: PatchValue<String?>? = null,
show all 16 lines

Remove channel memberships​

Remove channel memberships for a User.

Method(s)​

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

1pubnub.removeMemberships(
2 channels: List<String>,
3 userId: String? = null,
4 limit: Int? = null,
5 page: PNPage? = null,
6 filter: String? = null,
7 sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
8 include: MembershipInclude = MembershipInclude(),
9).async { result -> }
channels#
Type: List<String>
Default: no default
List of channels to remove from membership.
userId#
Type: String
Default: pubnub.configuration.userId.value
Unique User Metadata identifier.
If not supplied, then userId from configuration will be used.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMembershipKey>>,
Default: listOf()
Sort by CHANNEL_ID, CHANNEL_NAME, CHANNEL_UPDATED, CHANNEL_STATUS, CHANNEL_TYPE, UPDATED, STATUS, TYPE with asc/desc for sort direction (for example, PNSortKey.PNAsc(PNMembershipKey.TYPE)).
page#
Type: PNPage?
Default: null
Cursor-based pagination.
limit#
Type: Int
Default: 100
Number of objects to return. Default/Max: 100.
include#
Type: MembershipInclude
Default: MembershipInclude()
Whether to include additional fields.

Sample code​

1pubnub.removeMemberships(listOf("myChannel"))
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNChannelMembershipArrayResult(
2 val status: Int,
3 val data: Collection<PNChannelMembership>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNChannelMembership(
10 val channel: PNChannelMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>? = null,
15 val type: PatchValue<String?>? = null,
show all 16 lines

Manage channel memberships​

Manage a user's channel memberships.

Method(s)​

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

1pubnub.manageMemberships(
2 channelsToSet: List<PNChannelWithCustom>,
3 channelsToRemove: List<String>,
4 userId: String? = null,
5 limit: Int? = null,
6 page: PNPage? = null,
7 filter: String? = null,
8 sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
9 include: MembershipInclude = MembershipInclude(),
10).async { result -> }
channelsToSet#
Type: List<PNChannelWithCustom>
Default: no default
List of PNChannelWithCustom to add to membership.
channelsToRemove#
Type: List<String>
Default: no default
List of channels to remove from membership.
userId#
Type: String
Default: pubnub.configuration.userId.value
Unique User Metadata identifier.
If not supplied, then userId from configuration will be used.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMembershipKey>>,
Default: listOf()
List of properties to sort by. Available options are PNMembershipKey.CHANNEL_ID, PNMembershipKey.CHANNEL_NAME, PNMembershipKey.CHANNEL_UPDATED, PNMembershipKey.CHANNEL_STATUS, PNMembershipKey.CHANNEL_TYPE, PNMembershipKey.UPDATED, PNMembershipKey.STATUS and PNMembershipKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMembershipKey.TYPE) or PNSortKey.PNDesc(PNMembershipKey.STATUS).
page#
Type: PNPage?
Default: null
The paging object used for pagination.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
include#
Type: MembershipInclude
Default: MembershipInclude()
Object holding the configuration for whether to include additional data in the response.

Sample code​

1pubnub.manageMemberships(
2 channelsToSet = listOf(PNChannelMembership.Partial(channelId = "myChannelToSet")),
3 channelsToRemove = listOf("myChannelToRemove"),
4 include = MembershipInclude(
5 includeCustom = true,
6 includeStatus = true,
7 includeType = true,
8 includeChannel = true,
9 includeChannelCustom = true,
10 includeChannelType = true,
11 includeChannelStatus = true,
12 includeTotalCount = true
13 )
14).async { result ->
15 result.onFailure { exception ->
show all 20 lines

Response​

1data class PNChannelMembershipArrayResult(
2 val status: Int,
3 val data: Collection<PNChannelMembership>,
4 val totalCount: Int?,
5 val next: PNPage?,
6 val prev: PNPage?
7)
8
9data class PNChannelMembership(
10 val channel: PNChannelMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>? = null,
15 val type: PatchValue<String?>? = null,
show all 16 lines

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

1pubnub.getChannelMembers(
2 channel: String,
3 limit: Int? = null,
4 page: PNPage? = null,
5 filter: String? = null,
6 sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
7 include: MemberInclude = MemberInclude(),
8).async { result -> }
channel#
Type: String
Default: no default
Channel name.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMemberKey>>
Default: listOf()
List of properties to sort by. Available options are PNMemberKey.UUID_ID, PNMemberKey.UUID_NAME,PNMemberKey.UUID_UPDATED, PNMemberKey.UUID_NAME,PNMemberKey.UUID_TYPE, PNMemberKey.UPDATED,PNMemberKey.STATUS, PNMemberKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMemberKey.TYPE) or PNSortKey.PNDesc(PNMemberKey.STATUS).
include#
Type: MemberInclude
Default: MemberInclude()
Object holding the configuration for whether to include additional data in the response.

Sample code​

1pubnub.getChannelMembers("myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }

Response​

1data class PNMemberArrayResult(
2 val status: Int,
3 val data: Collection<PNMember>,
4 val totalCount: Int?,
5 val next: PNPage.PNNext?,
6 val prev: PNPage.PNPrev?
7)
8
9data class PNMember(
10 val uuid: PNUUIDMetadata?,
11 val custom: Any? = null,
12 val updated: Instant,
13 val eTag: String
14)

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

1pubnub.setChannelMembers(
2 channel: String,
3 users: List<PNMember.Partial>,
4 limit: Int? = null,
5 page: PNPage? = null,
6 filter: String? = null,
7 sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
8 include: MemberInclude = MemberInclude(),
9).async { result, status ->
10 if (status.error) {
11 // handle error
12 } else if (result != null) {
13 // handle result
14 }
15}
required
channel required#
Type: String
Default: no default
Channel name.
users#
Type: List<PNMember.Partial>
Default: no default
List of members PNMember.Partial to add to channel.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMemberKey>>
Default: listOf()
List of properties to sort by. Available options are PNMemberKey.UUID_ID, PNMemberKey.UUID_NAME,PNMemberKey.UUID_UPDATED, PNMemberKey.UUID_NAME,PNMemberKey.UUID_TYPE, PNMemberKey.UPDATED,PNMemberKey.STATUS, PNMemberKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMemberKey.TYPE) or PNSortKey.PNDesc(PNMemberKey.STATUS).
include#
Type: MemberInclude
Default: MemberInclude()
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.

Sample code​

1pubnub.setChannelMembers(
2 channel = "myChannel",
3 users = listOf(
4 PNMember.Partial(
5 uuidId = "myUserId",
6 custom = mapOf("custom" to "customValue"),
7 status = "status",
8 type = "typeABC"
9 )
10 ),
11 include = MemberInclude(includeUser = true, includeStatus = true, includeType = true),
12 sort = listOf(PNSortKey.PNAsc(PNMemberKey.STATUS)),
13).async { result ->
14 result.onFailure { exception ->
15 // Handle error
show all 19 lines

Response​

1data class PNMemberArrayResult(
2 val status: Int,
3 val data: Collection<PNMember>,
4 val totalCount: Int?,
5 val next: PNPage.PNNext?,
6 val prev: PNPage.PNPrev?
7)
8
9data class PNMember(
10 val uuid: PNUUIDMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>?,
15 val type: PatchValue<String?>?,
show all 16 lines

Remove channel members​

Remove members from a Channel.

Method(s)​

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

1pubnub.removeChannelMembers(
2 userIds: List<String>,
3 channel: String,
4 limit: Int? = null,
5 page: PNPage? = null,
6 filter: String? = null,
7 sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
8 include: MemberInclude = MemberInclude(),
9).async { result -> }
userIds#
Type: List<String>
Default: no default
List of members userIDs to remove from channel.
channel#
Type: String
Default: no default
Channel name.
limit#
Type: Int
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMemberKey>>
Default: listOf()
List of properties to sort by. Available options are PNMemberKey.UUID_ID, PNMemberKey.UUID_NAME,PNMemberKey.UUID_UPDATED, PNMemberKey.UUID_NAME,PNMemberKey.UUID_TYPE, PNMemberKey.UPDATED,PNMemberKey.STATUS, PNMemberKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMemberKey.TYPE) or PNSortKey.PNDesc(PNMemberKey.STATUS).
include#
Type: MemberInclude
Default: MemberInclude()
Object holding the configuration for whether to include additional data in the response.

Sample code​

1pubnub.removeChannelMembers(
2 channel = "myChannel",
3 userIds = listOf("myUserId"),
4 include = MemberInclude(includeUser = true, includeStatus = true, includeType = true),
5).async { result ->
6 result.onFailure { exception ->
7 // Handle error
8 }.onSuccess { value ->
9 // Handle successful method result
10 }
11}

Response​

1data class PNMemberArrayResult(
2 val status: Int,
3 val data: Collection<PNMember>,
4 val totalCount: Int?,
5 val next: PNPage.PNNext?,
6 val prev: PNPage.PNPrev?
7)
8
9data class PNMember(
10 val uuid: PNUUIDMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>?,
15 val type: PatchValue<String?>?,
show all 16 lines

Manage channel members​

Set and Remove channel memberships for a user.

Method(s)​

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

1pubnub.manageChannelMembers(
2 channel: String,
3 usersToSet: List<PNMember.Partial>,
4 userIdsToRemove: List<String>,
5 limit: Int? = null,
6 page: PNPage? = null,
7 filter: String? = null,
8 sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
9 include: MemberInclude = MemberInclude(),
10).async { result, status ->
11 if (status.error) {
12 // Handle error
13 } else if (result != null) {
14 // Handle successful result
15 }
show all 16 lines
channel#
Type: String
Default: no default
Channel name.
usersToSet#
Type: List<PNMember.Partial>
Default: no default
List of members PNMember.Partial to add to the channel with optional custom metadata.
userIdsToRemove#
Type: List<String>
Default: no default
List of members userIds to remove from the channel.
limit#
Type: Int?
Default: 100
The number of objects to retrieve at a time.
page#
Type: PNPage?
Default: null
The paging object used for pagination.
filter#
Type: String?
Default: null
Filter expression. Only matching objects are returned. See filtering.
sort#
Type: Collection<PNSortKey<PNMemberKey>>
Default: listOf()
List of properties to sort by. Available options are PNMemberKey.UUID_ID, PNMemberKey.UUID_NAME,PNMemberKey.UUID_UPDATED, PNMemberKey.UUID_NAME,PNMemberKey.UUID_TYPE, PNMemberKey.UPDATED,PNMemberKey.STATUS, PNMemberKey.TYPE. Use PNSortKey.PNAsc or PNSortKey.PNDesc to specify sort direction. For example: PNSortKey.PNAsc(PNMemberKey.TYPE) or PNSortKey.PNDesc(PNMemberKey.STATUS).
include#
Type: MemberInclude
Default: MemberInclude()
Object holding the configuration for whether to include additional data in the response.

Sample code​

1val pnMemberArrayResult: PNMemberArrayResult = pubnub.manageChannelMembers(
2 channel = "myChannel",
3 usersToSet = listOf(PNMember.Partial(uuidId = "myUserId", status = "myStatus", type = "distinctType")),
4 userIdsToRemove = listOf(),
5 include = MemberInclude(includeStatus = true, includeType = true)
6).sync()

Response​

1data class PNMemberArrayResult(
2 val status: Int,
3 val data: Collection<PNMember>,
4 val totalCount: Int?,
5 val next: PNPage.PNNext?,
6 val prev: PNPage.PNPrev?
7)
8
9data class PNMember(
10 val uuid: PNUUIDMetadata,
11 val custom: PatchValue<Map<String, Any?>?>? = null,
12 val updated: String,
13 val eTag: String,
14 val status: PatchValue<String?>?,
15 val type: PatchValue<String?>?,
show all 16 lines

Was this page useful?

Last updated on