Presence 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.
Presence lets you track who is online or offline and store custom state information. Presence shows:
- When a user has joined or left a channel
- How many users are subscribed to a particular channel (occupancy)
- Which channels a user or device is subscribed to
- Presence state associated with these users
Learn more about our Presence feature in the Presence overview.
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
}
}
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
Cache
This method has a 3-second response cache time.
Method(s)
To call Here Now
you can use the following methods in the Kotlin SDK:
pubnub.hereNow(
channels: List<String>,
channelGroups: List<String>,
includeState: Boolean,
includeUUIDs: Boolean
).async { result -> }
Parameter | Description |
---|---|
channels Type: List<String> Default: n/a | The channels to get the 'here now' details of. |
channelGroups Type: List<String> Default: n/a | The channelGroups to get the 'here now' details of. Wildcards are not supported. |
includeState Type: Boolean Default: false | If true , the response will include the presence states of the users for channels /channelGroups . |
includeUUIDs Type: Boolean Default: true | If true, the response will include the UUIDs of the connected clients. |
Sample code
Reference code
Get a list of UUIDs subscribed to channel
Returns
The hereNow()
operation returns a PNHereNowResult?
which contains the following operations:
Method | Description |
---|---|
totalChannels Type: Int | Total channels |
totalOccupancy Type: Int | Total occupancy |
channels Type: Map<String, PNHereNowChannelData> | A map with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details. |
PNHereNowChannelData
Method | Description |
---|---|
channelName Type: String | Channel name |
occupancy Type: Int | Occupancy of the channel |
occupants Type: List<PNHereNowOccupantData> | A list of PNHereNowOccupantData , see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
Method | Description |
---|---|
uuid Type: String | UUID of the user |
state Type: JsonElement? | State of the user |
Other examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Example response:
.async { result: Result<PNHereNowResult> ->
result.onSuccess { res: PNHereNowResult ->
res.channels.values.forEach { channelData ->
channelData.channelName // ch1
channelData.occupancy // 3
channelData.occupants.forEach { o ->
o.uuid // some_uuid, returned by default
o.state // {"data":{"isTyping":true}}, requested
}
}
}.onFailure { e ->
// handle error
e.message
e.statusCode
e.pubnubError
show all 17 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can return only the occupancy
information for a single channel by specifying the channel and setting UUIDs
to false:
Example response:
.async { result: Result<PNHereNowResult> ->
result.onSuccess { res: PNHereNowResult ->
res.channels.values.forEach { channelData ->
channelData.channelName // ch1
channelData.occupancy // 3
}
}.onFailure { e ->
// handle error
e.message
e.statusCode
e.pubnubError
}
}
Here now for channel groups
Example response:
.async { result: Result<PNHereNowResult> ->
result.onSuccess { res: PNHereNowResult ->
res.totalOccupancy
}.onFailure { e ->
// handle error
e.message
e.statusCode
e.pubnubError
}
}
Where now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns the list of channels a UUID is subscribed to.
Timeout events
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
To call whereNow()
you can use the following method(s) in the Kotlin SDK:
pubnub.whereNow(
uuid: String
).async { result -> }
Parameter | Description |
---|---|
uuid Type: String Default: n/a | UUID of the user to get its current channel subscriptions. |
Sample code
You simply need to define the uuid
and the callback function to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to
Returns
The whereNow()
operation returns a PNWhereNowResult?
which contains the following operations:
Method | Description |
---|---|
channels Type: List<String> | List of channels where the uuid is present. |
Other examples
Obtain information about the current list of channels of some other UUID
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.
The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.
Presence state format
Presence state must be expressed as a JsonObject. When calling setState
, be sure to supply an initialized JsonObject or POJO which can be serialized to a JsonObject.
Method(s)
To set the state, call setPresenceState()
you can use the following method(s) in the Kotlin SDK:
Set state
pubnub.setPresenceState(
channels: List<String>,
channelGroups: List<String>,
state: Any,
uuid: String
).async { result -> }
Parameter | Description |
---|---|
channels Type: List<String> Default: n/a | Channels to set state to. |
channelGroups Type: List<String> Default: n/a | Channel groups to set state to. |
state Type: Any Default: n/a | The state to set. |
uuid Type: String Default: n/a | The UUID to set state for. |
Get state
pubnub.getPresenceState(
channels: List<String>,
channelGroups: List<String>,
uuid: String
).async { result -> }
To get the state, call getPresenceState()
you can use the following method(s) in the Kotlin SDK:
Parameter | Description |
---|---|
channels Type: List<String> Default: n/a | Channels to get state of. |
channelGroups Type: List<String> Default: n/a | Channel groups to get state of. |
uuid Type: String Default: n/a | The UUID to get state for. |
Sample code
Returns
The setPresenceState()
operation returns a PNSetStateResult?
which contains the following operations:
Method | Description |
---|---|
state Type: JsonElement | The actual state object |
The getPresenceState()
operation returns a PNSetStateResult?
which contains the following operations:
Method | Description |
---|---|
stateByUUID Type: Map<String, JsonElement> | Map of UUIDs and the user states. |
Other examples
Set state for channels in channel group
Get state for UUID