App Context API for PubNub Kotlin SDK
Breaking changes since 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 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 set or removed from the database. Clients can receive these events in real time and update their front-end application accordingly.
Calling Kotlin methods
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 choose one of these or the operation will not be performed at all.
For example, the following code is valid and will compile, but the publish won't be performed:
pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
)
To successfully publish a message, you must follow the actual method invocation with whether to perform it synchronously or asynchronously, for example:
pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
).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> = listOf(),
page: PNPage? = null,
limit: Int? = null,
includeCustom: Boolean = false,
includeCount: Boolean = false
).async { result, status -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
filter | String? | Optional | null | 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 id , name , and updated . Use asc or desc to specify sort direction. For example: {name: 'asc'} |
page | PNPage? | Optional | null | The paging object used for pagination. |
limit | Int | Optional | 100 | The number of objects to retrieve at a time. |
includeCustom | Boolean | Optional | false | Whether to include the Custom field in the fetch response. |
includeCount | Boolean | Optional | false | Request IncludeCount to be included in paginated response. By default, IncludeCount is omitted. |
Basic Usage
pubnub.getAllUUIDMetadata()
.async { result, status ->
if (status.error) {
//handle error
} else if (result != null) {
//handle result
}
}
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: String? = null,
val externalId: String?,
val profileUrl: String?,
val email: String?,
val custom: Any?,
show all 18 linesGet User Metadata
Returns metadata for the specified UUID, optionally including the custom data object for each.
Method(s)
To Get UUID Metadata
you can use the following method(s) in the Kotlin SDK:
pubnub.getUUIDMetadata(
uuid: String? = null,
includeCustom: Boolean = false
).async { result, status -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
uuid | String | Optional | pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
includeCustom | Boolean | Optional | false | Whether to include the custom field in the fetch response. |
Basic Usage
pubnub.getUUIDMetadata()
.async { result, status ->
if (status.error) {
//handle error
} else if (result != null) {
//handle result
}
}
Response
data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?
)
data class PNUUIDMetadata(
val id: String,
val name: String? = null,
val externalId: String?,
val profileUrl: String?,
val email: String?,
val custom: Any?,
val updated: Instant?,
val eTag: String?
)
Set User Metadata
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,
).async { result, status -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
uuid | String | Optional | pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
includeCustom | Boolean | Optional | false | Whether to include the custom field in the fetch response. |
name | String? | Optional | null | Display name for the user. |
externalId | String? | Optional | null | User's identifier in an external system |
profileUrl | String? | Optional | null | The URL of the user's profile picture |
email | String? | Optional | null | The user's email address. |
custom | Any? | Optional | null | Any object of key-value pairs with supported data types. App Context filtering language doesn’t support filtering by custom properties. |
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, status ->
if (status.error) {
//handle error
} else if (result != null) {
//handle result
}
}
Response
data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?
)
data class PNUUIDMetadata(
val id: String,
val name: String? = null,
val externalId: String?,
val profileUrl: String?,
val email: String?,
val custom: Any?,
val updated: Instant?,
val eTag: String?
)
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, status -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
uuid | String | Optional | pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
Basic Usage
pubnub.removeUUIDMetadata()
.async { result, status ->
if (status.error) {
//handle error
} else if (result != null) {
//handle 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> = listOf(),
page: PNPage? = null,
limit: Int? = null,
includeCustom: Boolean = false,
includeCount: Boolean = false,
).async { result, status -> }
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
filter | String? | Optional | null | 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 id , name , and updated . Use asc or desc to specify sort direction. For example: {name: 'asc'} |
page | PNPage? | Optional | null | The paging object used for pagination. |
limit | Int | Optional | 100 | The number of objects to retrieve at a time. |
includeCustom | Boolean | Optional | false | Whether to include the Custom field in the fetch response. |
includeCount | Boolean | Optional | false | Request IncludeCount to be included in paginated response. By default, IncludeCount is omitted. |
Basic Usage
pubnub.getAllChannelMetadata()
.async { result, status ->
if (status.error) {
//handle error
} else if (result != null) {
//handle 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: String?,
val description: String?,
val custom: Any?,
val updated: Instant?,
val eTag: String?
show all 16 linesGet Channel Metadata
Returns metadata for the specified Channel, optionally including the custom data object for each.