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)
channels#- The
channelsto get thehere nowdetails. channelGroups#- The
channelGroupsto get thehere nowdetails. Wildcards are not supported. includeState#- If
true, the response will include the presence states of the users forchannels/channelGroups includeUUIDs#- If
true, the response will include theUUIDsof the connected clients. limit#- Maximum number of occupants to return per channel. Valid range:
0-1000. The server enforces the maximum of1000and rejects values outside this range. Use0to get occupancy counts without user details (whenlimit=0, the server omits UUIDs entirely). offset#- Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be
>= 0. Requireslimit > 0. Server rejects negative values. Use withlimitto paginate through large occupant lists. asyncrequired#Consumerof aResultof typePNHereNowResult
Sample code
Get a list of UUIDs subscribed to channel
1
Returns
The hereNow() operation returns a PNHereNowResult which contains the following operations:
getTotalChannels()#- Total
Channels. getTotalOccupancy()#- Total
Occupancy. getChannels()#- A map with values of
PNHereNowChannelDatafor eachchannel. See PNHereNowChannelData for more details.
PNHereNowChannelData
getChannelName()#Channelname.getOccupancy()#Occupancyof thechannel.getOccupants()#- A list of
PNHereNowOccupantData, see PNHereNowOccupantData for more details.
PNHereNowOccupantData
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)
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:
getChannels()#- The list of
channelswhere theUUIDis 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)
Get state
1this.pubnub.getPresenceState()
2 .channels(Arrays)
3 .channelGroups(Arrays)
4 .uuid(String)
Sample code
Set state
1
Get state
1
Returns
The setPresenceState() operation returns a PNSetStateResult which contains the following operations:
getState()#- Map of
UUIDsand the user states.
The getPresenceState() operation returns a PNGetStateResult which contains the following operations:
getStateByUUID()#- Map of
UUIDsand 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