Presence API for Java SDK
Breaking changes in v9.0.0
PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin 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 Java 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.
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 method(s) in the Java SDK:
1this.pubnub.hereNow()
2 .channels(Array)
3 .channelGroups(Arrays)
4 .includeState(true)
5 .includeUUIDs(true)
6 .limit(int)
7 .offset(Integer)
| Parameter | Description |
|---|---|
channelsType: Array Default: n/a | The channels to get the here now details. |
channelGroupsType: Arrays Default: n/a | The channelGroups to get the here now details. Wildcards are not supported. |
includeStateType: Boolean Default: false | If true, the response will include the presence states of the users for channels/channelGroups |
includeUUIDsType: Boolean Default: true | If true, the response will include the UUIDs of the connected clients. |
limitType: int Default: 1000 | Maximum number of occupants to return per channel. Valid range: 0-1000. The server enforces the maximum of 1000 and rejects values outside this range. Use 0 to get occupancy counts without user details (when limit=0, the server omits UUIDs entirely). |
offsetType: Integer Default: null | Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0. Requires limit > 0. Server rejects negative values. Use with limit to paginate through large occupant lists. |
async *Type: Consumer<Result>Default: n/a | Consumer of a Result of type PNHereNowResult |
Sample code
Reference code
Get a list of UUIDs subscribed to channel
1
Returns
The hereNow() operation returns a PNHereNowResult which contains the following operations:
| Method | Description |
|---|---|
getTotalChannels()Type: Int | Total Channels. |
getTotalOccupancy()Type: Int | Total Occupancy. |
getChannels()Type: Map <String, PNHereNowChannelData> | A map with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details. |
PNHereNowChannelData
| Method | Description |
|---|---|
getChannelName()Type: String | Channel name. |
getOccupancy()Type: Int | Occupancy of the channel. |
getOccupants()Type: List <PNHereNowOccupantData> | A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
| Method | Description |
|---|---|
getUuid()Type: String | UUIDs of the user. |
getState()Type: Object | 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.
1
Return 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:
1
Here now for channel groups
1
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 Java SDK:
1pubnub.whereNow()
2 .uuid(String)
| Parameter | Description |
|---|---|
uuidType: String | Uuid of the user we want to spy on. |
async *Type: Command | Execute as async. |
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
1
Returns
The whereNow() operation returns a PNWhereNowResult which contains the following operations:
| Method | Description |
|---|---|
getChannels()Type: List <String> | The list of channels where the UUID is present. |
Other examples
Obtain information about the current list of channels of some other UUID
1
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)
Set state
1this.pubnub.setPresenceState()
2 .channels(Array)
3 .channelGroups(Array)
4 .state(HashMap)
5 .uuid(String)
| Parameter | Description |
|---|---|
channelsType: Array | Channels to set state. |
channelGroupsType: Array | ChannelGroups to set state. |
stateType: HashMap | State to set. |
uuidType: String | Set state for specific UUID. |
async *Type: Consumer<Result> | Consumer of a Result of type PNSetStateResult. |
Get state
1this.pubnub.getPresenceState()
2 .channels(Arrays)
3 .channelGroups(Arrays)
4 .uuid(String)
| Parameter | Description |
|---|---|
channelsType: Arrays | Channel name to fetch the state. |
channelGroupsType: Arrays | ChannelGroups name to fetch the state. |
uuidType: String | UUID |
async *Type: Consumer<Result> | Consumer of a Result of type PNGetStateResult. |
Sample code
Set state
1
Get state
1
Returns
The setPresenceState() operation returns a PNSetStateResult which contains the following operations:
| Method | Description |
|---|---|
getState()Type: Map <String, Object> | Map of UUIDs and the user states. |
The getPresenceState() operation returns a PNGetStateResult which contains the following operations:
| Method | Description |
|---|---|
getStateByUUID()Type: Map <String, Object> | Map of UUIDs and the user states. |
Other examples
Set state for channels in channel group
1
The above code would return the following response to the client:
1