zendesk/radar — pull request #6 by mixu (2012-11-08 22:49:45+00:00)
Title: Adds per-client notifications to presence

Previously, presence only exposed events at the user level, e.g.: online / offline for each userId.

In some cases, we want to know how many clients are online and what the clientId's are.

For example, that makes it easy to elect one tab (one client) out of many open tabs to do something, like play a sound:
- get all the client IDs, sort them lexicographically and compare whether your clientId is the smallest
- if yes, you are the leader and should watch for client_online messages that are lexicographically larger to find out if some else becomes a leader,
- if no, then you are not and should wait for client_offline messages to determine if you become the leader.

If the client IDs are monotonically increasing (they are not in Engine.io), then this is even slightly simpler because the leader cannot be displaced by a later client.

Another advantage is that client_offline messages are instantaneous - for user disconnects that were ungraceful, there is a grace period until we send the user_offline message but we don't need to wait for that if the recipient understands client IDs.

I've also taken the opportunity to make the presence implementation clearer with this refactor.

TODO:

- I still need to add the client ID information into the sync() and REST API operations.
- Also, it is likely that this client ID information will be togglable per-presence-subscription so that if you don't care about it, you won't get granular messages. That will be added later on.