Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public interface HereNow extends Endpoint<PNHereNowResult> {
HereNow includeState(boolean includeState);

HereNow includeUUIDs(boolean includeUUIDs);

HereNow limit(int limit);

HereNow offset(Integer offset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,61 @@ public static void main(String[] args) throws PubNubException {

PubNub pubnub = PubNub.create(configBuilder.build());

// Get presence information for specified channels
// Get presence information for specified channels with pagination support
pubnub.hereNow()
.channels(Arrays.asList("coolChannel", "coolChannel2"))
.includeUUIDs(true)
.limit(100)
.async(result -> {
result.onSuccess((PNHereNowResult res) -> {
printHereNowResult(res);

System.out.println("Total Channels: " + res.getTotalChannels());
System.out.println("Total Occupancy: " + res.getTotalOccupancy());

for (PNHereNowChannelData channelData : res.getChannels().values()) {
System.out.println("---");
System.out.println("Channel: " + channelData.getChannelName());
System.out.println("Occupancy: " + channelData.getOccupancy());
System.out.println("Occupants:");
for (PNHereNowOccupantData occupant : channelData.getOccupants()) {
System.out.println("UUID: " + occupant.getUuid() + " State: " + occupant.getState());
}
// Check if more results are available
if (res.getNextOffset() != null) {
System.out.println("\nMore results available. Fetching next page...\n");

// Fetch next page using the offset from previous response
pubnub.hereNow()
.channels(Arrays.asList("coolChannel", "coolChannel2"))
.includeUUIDs(true)
.limit(100)
.offset(res.getNextOffset())
.async(result2 -> {
result2.onSuccess((PNHereNowResult res2) -> {
printHereNowResult(res2);

// Continue pagination if needed by checking res2.getNextOffset()
}).onFailure((PubNubException exception) -> {
System.out.println("Error retrieving hereNow data: " + exception.getMessage());
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it make sense / possible to create local closure which will be called from initial and nested hereNow calls, but in addition to result will pass offset for example and inside if it is not nil, make another hereNow calls - it will give user better idea because novice may think that it is ok to nest hereNow calls as long as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will modify this code.

}
}).onFailure( (PubNubException exception) -> {
}).onFailure((PubNubException exception) -> {
System.out.println("Error retrieving hereNow data: " + exception.getMessage());
});
});
}

/**
* Helper method to print HereNow presence information
*/
private static void printHereNowResult(PNHereNowResult result) {
System.out.println("Total Channels: " + result.getTotalChannels());
System.out.println("Total Occupancy: " + result.getTotalOccupancy());

for (PNHereNowChannelData channelData : result.getChannels().values()) {
System.out.println("---");
System.out.println("Channel: " + channelData.getChannelName());
System.out.println("Occupancy: " + channelData.getOccupancy());
System.out.println("Occupants:");
for (PNHereNowOccupantData occupant : channelData.getOccupants()) {
System.out.println("UUID: " + occupant.getUuid() + " State: " + occupant.getState());
}
}

if (result.getNextOffset() != null && result.getNextOffset() != 0) {
System.out.println("Next Offset: " + result.getNextOffset());
}
}
}
// snippet.end
Loading
Loading