App Context API for Kotlin SDK

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 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.

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.

val channel = pubnub.channel("channelName")

channel.publish("This SDK rules!").async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

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

pubnub.getAllUUIDMetadata(
filter: String? = null,
sort: Collection<PNSortKey<PNKey>> = listOf(),
page: PNPage? = null,
limit: Int? = null,
includeCustom: Boolean = false,
includeCount: Boolean = false
).async { result -> }
* required
ParameterDescription
filter
Type: String?
Default:
null
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.enums.PNLogVerbosity

/**
* This example demonstrates how to use the getAllUUIDMetadata method in the PubNub Kotlin SDK.
*
* The App Context (Objects) API allows you to store metadata about users (UUIDs) in PubNub's
* database without setting up your own backend.
*/
fun main() {
println("PubNub UUID Metadata Example")
println("============================")

show all 190 lines

Response

data class PNUUIDMetadataArrayResult(
val status: Int,
val data: Collection<PNUUIDMetadata>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNUUIDMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val externalId: PatchValue<String?>? = null,
val profileUrl: PatchValue<String?>? = null,
val email: PatchValue<String?>? = null,
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:

pubnub.getUUIDMetadata(
uuid: String? = null,
includeCustom: Boolean = false
).async { result -> }
* required
ParameterDescription
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.

Basic Usage

pubnub.getUUIDMetadata()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?
)

data class PNUUIDMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val externalId: PatchValue<String?>? = null,
val profileUrl: PatchValue<String?>? = null,
val email: PatchValue<String?>? = null,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: PatchValue<String>? = null,
val eTag: PatchValue<String>? = null,
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:

pubnub.setUUIDMetadata(
uuid: String? = null,
includeCustom: Boolean = false,
name: String? = null,
externalId: String? = null,
profileUrl: String? = null,
email: String? = null,
custom: Any? = null,
type: String?,
status: String?,
ifMatchesEtag: String?
).async { result -> }
* required
ParameterDescription
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:
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()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?
)

data class PNUUIDMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val externalId: PatchValue<String?>? = null,
val profileUrl: PatchValue<String?>? = null,
val email: PatchValue<String?>? = null,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: PatchValue<String>? = null,
val eTag: PatchValue<String>? = null,
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:

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

Basic Usage

pubnub.removeUUIDMetadata()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data 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.

Method(s)

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

pubnub.getAllChannelMetadata(
filter: String? = null,
sort: Collection<PNSortKey<PNKey>> = listOf(),
page: PNPage? = null,
limit: Int? = null,
includeCustom: Boolean = false,
includeCount: Boolean = false,
).async { result -> }
* required
ParameterDescription
filter
Type: String?
Default:
null
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.getAllChannelMetadata()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNChannelMetadataArrayResult(
val status: Int,
val data: Collection<PNChannelMetadata>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNChannelMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val description: PatchValue<String?>? = null,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: PatchValue<String>? = null,
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:

pubnub.getChannelMetadata(
channel: String,
includeCustom: Boolean = false
).async { result -> }
* required
ParameterDescription
channel
Type: String
Default:
n/a
Channel name.
includeCustom
Type: Boolean
Default:
false
Whether to include the custom field in the fetch response.

Basic Usage

pubnub.getChannelMetadata(channel = "myChannel")
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNChannelMetadataResult(
val status: Int,
val data: PNChannelMetadata?
)

data class PNChannelMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val description: PatchValue<String?>? = null,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: PatchValue<String>? = null,
val eTag: PatchValue<String>? = null,
val type: PatchValue<String?>? = null,
val status: PatchValue<String?>? = null,
)

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:

pubnub.setChannelMetadata(
channel: String,
includeCustom: Boolean = false,
name: String? = null,
description: String? = null,
custom: Any? = null
type: String?,
status: String?,
ifMatchesEtag: String?
).async { result -> }
* required
ParameterDescription
channel
Type: String
Default:
n/a
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
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:
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")
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNChannelMetadataResult(
val status: Int,
val data: PNChannelMetadata?
)

data class PNChannelMetadata(
val id: String,
val name: PatchValue<String?>? = null,
val description: PatchValue<String?>? = null,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: PatchValue<String>? = null,
val eTag: PatchValue<String>? = null,
val type: PatchValue<String?>? = null,
val status: PatchValue<String?>? = null,
)

Other Examples

Iteratively update existing metadata
import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadataResult
import com.pubnub.api.v2.PNConfiguration
import org.junit.Assert.assertEquals

fun main() {

// create configuration
val pnConfiguration = PNConfiguration.builder(userId = UserId("user01"), subscribeKey = "demo") {
publishKey = "demo"
}.build()

// create PubNub
val pubNub = PubNub.create(pnConfiguration)
show all 57 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:

pubnub.removeChannelMetadata(
channel: String
).async { result -> }
* required
ParameterDescription
channel
Type: String
Default:
n/a
Channel name.

Basic Usage

pubnub.removeChannelMetadata(channel = "myChannel")
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data 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:

pubnub.getMemberships(
userId: String? = null,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMembershipKey>> = listOf(),
include: MembershipInclude = MembershipInclude(),
).async { result -> }
* required
ParameterDescription
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.getMemberships(
include = MembershipInclude(
includeCustom = true,
includeStatus = true,
includeType = true,
includeTotalCount = true,
includeChannel = true,
includeChannelCustom = true,
includeChannelType = true,
includeChannelStatus = true
),
sort = listOf(PNSortKey.PNAsc(PNMembershipKey.CHANNEL_ID))
).async { result ->
result.onFailure { exception ->
// Handle error
show all 19 lines

Response

data class PNChannelMembershipArrayResult(
val status: Int,
val data: Collection<PNChannelMembership>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>? = null,
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:

pubnub.setMemberships(
channels: List<ChannelMembershipInput>,
userId: String? = null,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
include: MembershipInclude = MembershipInclude(),
).async { result -> }
* required
ParameterDescription
channelsList 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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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

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

Basic Usage

pubnub.setMemberships(
channels = listOf(PNChannelMembership.Partial(channel = "myChannel", custom = mapOf("owner" to "PubNub"), status = "active", type = "regular_membership")),
include = MembershipInclude(
includeCustom = true,
includeStatus = true,
includeType = true,
includeChannel = true,
includeChannelCustom = true,
includeChannelType = true,
includeChannelStatus = true,
includeTotalCount = true
),
).async { result ->
result.onFailure { exception ->
// Handle error
show all 19 lines

Response

data class PNChannelMembershipArrayResult(
val status: Int,
val data: Collection<PNChannelMembership>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>? = null,
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:

pubnub.removeMemberships(
channels: List<String>,
userId: String? = null,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
include: MembershipInclude = MembershipInclude(),
).async { result -> }
* required
ParameterDescription
channels
Type: List<String>
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.removeMemberships(listOf("myChannel"))
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNChannelMembershipArrayResult(
val status: Int,
val data: Collection<PNChannelMembership>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>? = null,
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:

pubnub.manageMemberships(
channelsToSet: List<PNChannelWithCustom>,
channelsToRemove: List<String>,
userId: String? = null,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMembershipKey>>, = listOf(),
include: MembershipInclude = MembershipInclude(),
).async { result -> }
* required
ParameterDescription
channelsToSet
Type: List<PNChannelWithCustom>
Default:
n/a
List of PNChannelWithCustom to add to membership.
channelsToRemove
Type: List<String>
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.manageMemberships(
channelsToSet = listOf(PNChannelWithCustom(channel = "myChannelToSet")),
channelsToRemove = listOf("myChannelToRemove"),
include = MembershipInclude(
includeCustom = true,
includeStatus = true,
includeType = true,
includeChannel = true,
includeChannelCustom = true,
includeChannelType = true,
includeChannelStatus = true,
includeTotalCount = true
)
).async { result ->
result.onFailure { exception ->
show all 20 lines

Response

data class PNChannelMembershipArrayResult(
val status: Int,
val data: Collection<PNChannelMembership>,
val totalCount: Int?,
val next: PNPage?,
val prev: PNPage?
)

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>? = null,
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:

pubnub.getChannelMembers(
channel: String,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
include: MemberInclude = MemberInclude(),
).async { result -> }
* required
ParameterDescription
channel
Type: String
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.getChannelMembers()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNMemberArrayResult(
val status: Int,
val data: Collection<PNMember>,
val totalCount: Int?,
val next: PNPage.PNNext?,
val prev: PNPage.PNPrev?
)

data class PNMember(
val uuid: PNUUIDMetadata?,
val custom: Any? = null,
val updated: Instant,
val eTag: String
)

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:

pubnub.setChannelMembers(
channel: String,
users: List<PNMember.Partial>,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
include: MemberInclude = MemberInclude(),
).async { result, status ->
if (status.error) {
// handle error
} else if (result != null) {
// handle result
}
}
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.
users
Type: List<PNMember.Partial>
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.setChannelMembers(
channel = "myChannel",
users = listOf(
PNMember.Partial(uuid = "myUserId", custom = mapOf("custom" to "customValue"), status = "status", type = "typeABC")
),
include = MemberInclude(includeUser = true, includeStatus = true, includeType = true),
sort = listOf(PNSortKey.PNAsc(PNMemberKey.STATUS)),
).async { result, status ->
if (status.error) {
// Handle error
} else if (result != null) {
// Handle successful method result
}
}

Response

data class PNMemberArrayResult(
val status: Int,
val data: Collection<PNMember>,
val totalCount: Int?,
val next: PNPage.PNNext?,
val prev: PNPage.PNPrev?
)

data class PNMember(
val uuid: PNUUIDMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>?,
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:

pubnub.removeChannelMembers(
userIds: List<String>,
channel: String,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
include: MemberInclude = MemberInclude(),
).async { result -> }
* required
ParameterDescription
userIds
Type: List<String>
Default:
n/a
List of members userIDs to remove from channel.
channel
Type: String
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.removeChannelMembers(
channel = "myChannel",
userIds = listOf("myUserId"),
include = MemberInclude(includeUser = true, includeStatus = true, includeType = true),
).async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

Response

data class PNMemberArrayResult(
val status: Int,
val data: Collection<PNMember>,
val totalCount: Int?,
val next: PNPage.PNNext?,
val prev: PNPage.PNPrev?
)

data class PNMember(
val uuid: PNUUIDMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>?,
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:

pubnub.manageChannelMembers(
channel: String,
usersToSet: List<PNMember.Partial>,
userIdsToRemove: List<String>,
limit: Int? = null,
page: PNPage? = null,
filter: String? = null,
sort: Collection<PNSortKey<PNMemberKey>> = listOf(),
include: MemberInclude = MemberInclude(),
).async { result, status ->
if (status.error) {
// Handle error
} else if (result != null) {
// Handle successful result
}
show all 16 lines
* required
ParameterDescription
channel
Type: String
Default:
n/a
Channel name.
usersToSet
Type: List<PNMember.Partial>
Default:
n/a
List of members PNMember.Partial to add to the channel with optional custom metadata.
userIdsToRemove
Type: List<String>
Default:
n/a
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
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
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.

Basic Usage

pubnub.setChannelMembers(
channel = channel,
usersToSet = listOf(PNMember.Partial(uuidId = testUserId01, status = status01, type = type01)),
userIdsToRemove = listOf("myUserToRemove"),
include = MemberInclude(includeStatus = true, includeType = true)
).async { result, status ->
if (status.error) {
// Handle error
} else if (result != null) {
result.channels.values.forEach { channelData ->
println("---")
println("Channel: ${channelData.channelName}")
println("Occupancy: ${channelData.occupancy}")
println("Occupants:")
channelData.occupants.forEach { o ->
show all 20 lines

Response

data class PNMemberArrayResult(
val status: Int,
val data: Collection<PNMember>,
val totalCount: Int?,
val next: PNPage.PNNext?,
val prev: PNPage.PNPrev?
)

data class PNMember(
val uuid: PNUUIDMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: PatchValue<String?>?,
val type: PatchValue<String?>?,
show all 16 lines
Last updated on