PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

Spaces API for PubNub JavaScript SDK

A Space is where online interactions within a given business domain happen. A Space may, for example, be an abstract representation of your home (for IoT device purposes), a chat channel about football (in chat applications), or a multiplayer game instance.

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!",
    spaceId = "my_space"
)

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!",
    spaceId = "my_space"
).async { result, status ->
    if (status.error) {
        // handle error
    } else {
        // handle successful method result
    }
}

Fetch Spaces

Returns a paginated list of Spaces, optionally including custom fields for each.

Method(s)

pubnub.fetchSpaces(
    limit: Int? = null,
    page: PNPage? = null,
    filter: String? = null,
    sort: Collection<PNSortKey> = listOf(),
    includeCount: Boolean = false,
    includeCustom: Boolean = false
).async { result, status -> }
ParameterTypeRequiredDefaultDescription
limitIntOptional100The number of objects to retrieve at a time.
pagePNPage?OptionalnullThe paging object used for pagination.
filterString?OptionalnullExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
sortList<PNSortKey>OptionallistOf()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'}
includeCountBooleanOptionalfalseRequest IncludeCount to be included in paginated response. By default, IncludeCount is omitted.
includeCustomBooleanOptionalfalseWhether to include the Custom field in the fetch response.

Basic Usage

pubnub.fetchSpaces()
        .async { result, status ->
            if (status.error) {
                //handle error
            } else if (result != null) {
                //handle result
            }
        }

Response

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

data class PNSpace(
    val id: String,
    val name: String?,
    val description: String?,
    val custom: Any?,
    val updated: String?,
    val eTag: String?
)

Fetch Space

Returns metadata for the specified Space, optionally including custom fields for each.

Method(s)

pubnub.fetchSpace(
    spaceId: String,
    includeCustom: Boolean = false
).async { result, status -> }
ParameterTypeRequiredDefaultDescription
spaceIdStringyesSpace ID.
includeCustomBooleanOptionalfalseWhether to include the custom field in the fetch response.

Basic Usage

pubnub.fetchSpace(spaceid = "mySpace")
        .async { result, status ->
            if (status.error) {
                //handle error
            } else if (result != null) {
                //handle result
            }
        }

Response

data class PNSpaceResult(
    val status: Int,
    val data: PNSpace?
)

data class PNSpace(
    val id: String,
    val name: String?,
    val description: String?,
    val custom: Any?,
    val updated: String?,
    val eTag: String?
)

Create Space

Create metadata for a Space in the database, optionally including custom fields for each.

Method(s)

pubnub.createSpace(
    spaceId: String,
    name: String? = null,
    description: String? = null,
    custom: Any? = null,
    includeCustom: Boolean = false
).async { result, status -> }
ParameterTypeRequiredDefaultDescription
spaceIdStringyesSpace ID.
nameString?OptionalnullName for the Space.
descriptionString?OptionalnullDescription of the Space.
customAny?OptionalnullAny object of key-value pairs with supported data types. Objects filtering language doesn’t support filtering by custom properties.
includeCustomBooleanOptionalfalseWhether to include the custom field in the fetch response.

Basic Usage

pubnub.createSpace(spaceId = "mySpace")
        .async { result, status ->
            if (status.error) {
                //handle error
            } else if (result != null) {
                //handle result
            }
        }

Response

data class PNSpaceResult(
    val status: Int,
    val data: PNSpace?
)

data class PNSpace(
    val id: String,
    val name: String?,
    val description: String?,
    val custom: Any?,
    val updated: String?,
    val eTag: String?
)

Update Space

Update existing metadata for a Space in the database, optionally including custom fields for each.

Method(s)

pubnub.updateSpace(
    spaceId: String,
    name: String? = null,
    description: String? = null,
    custom: Any? = null,
    includeCustom: Boolean = false
).async { result, status -> }
ParameterTypeRequiredDefaultDescription
spaceIdStringyesSpace ID.
nameString?OptionalnullName for the Space.
descriptionString?OptionalnullDescription of the Space.
customAny?OptionalnullAny object of key-value pairs with supported data types. Objects filtering language doesn’t support filtering by custom properties.
includeCustomBooleanOptionalfalseWhether to include the custom field in the fetch response.

Basic Usage

pubnub.updateSpace(spaceId = "mySpace")
        .async { result, status ->
            if (status.error) {
                //handle error
            } else if (result != null) {
                //handle result
            }
        }

Response

data class PNSpaceResult(
    val status: Int,
    val data: PNSpace?
)

data class PNSpace(
    val id: String,
    val name: String?,
    val description: String?,
    val custom: Any?,
    val updated: String?,
    val eTag: String?
)

Remove Space

Removes the metadata from a specified Space.

Method(s)

pubnub.removeSpace(
    spaceId: String
).async { result, status -> }
ParameterTypeRequiredDefaultDescription
spaceIdStringyesSpace ID.

Basic Usage

pubnub.removeSpace(spaceId = "mySpace")
        .async { result, status ->
            if (status.error) {
                //handle error
            } else if (result != null) {
                //handle result
            }
        }

Response

data class PNRemoveSpaceResult(val status: Int)
  • Fetch Spaces
  • Fetch Space
  • Create Space
  • Update Space
  • Remove Space
© PubNub Inc. - Privacy Policy