Implement real-time presence tracking with PubNub
79
Quality
67%
Does it follow best practices?
Impact
98%
1.07xAverage score across 5 eval scenarios
You are building a chat application with many rooms. The room listing page shows users how many people are currently in each room. This page may be viewed frequently by many users, and the list of rooms can be long (50-100 rooms).
Fetching live occupancy data from PubNub every time the page renders or a component re-renders is expensive in terms of API calls and latency. The occupancy counts do not need to be perfectly real-time on this listing page -- being within 30 seconds of accurate is acceptable.
The listing page only needs the occupancy count for each room. It does not need to know the individual UUIDs or their state metadata.
Implement a JavaScript class called PresenceCache that:
getOccupancy(channel) method that returns the current occupancy for a given channelA single JavaScript file presence-cache.js containing the PresenceCache class.