8 min readUpdated Nov 29, 2022

User presence at scale

User presence at scale
Jo StichburyJo Stichbury

At its most simple, user presence represents a user’s online or offline status and is typically used to show whether a user is available, for example, on a chat system. Here are some other examples of where it’s used:

  • For audience engagement: members on a Twitch livestream see who is online to share the experience, to send each other emoji reactions or gif memes.
  • If a gamer spots that a friend is online on Discord, they can message them to join a particular game lobby.
  • In medical apps, a doctor can be notified when a patient has entered the online waiting room so that they can prepare for the appointment.
  • In education software, a teacher may be notified that a student has entered the virtual classroom so they can interact with them individually before a class begins.
  • In online collaboration tools such as Google docs, as someone works in a document, they can see who else has opened the file to work on it.

Presence is a key feature of live apps and services and a primary element of the user experience in audience engagement. It is a great way to build a sense of immediacy between participants; knowing who else is online builds a sense of inclusion and interaction.

Besides indicating availability, presence can display the user’s status. For example, if the user’s account has been inactive for a certain number of minutes, they may be set as “Away”. Alternatively, the user can customize a status message or apply one from a set and use emojis to indicate what they are doing. Within an organization, the use of a specific set of status updates builds a common lexicon amongst participants and gives a sense of community.

Some systems allow status information to include details such as the user’s current location, or whether they are currently typing a message, and offer alerts when the presence information of a user changes.

How to monitor user presence with Ably

Ably powers synchronized digital experiences like virtual live events and synchronized collaboration and offers feature-complete messaging, including features like presence. Ably is an event-driven system that uses a scalable and resilient messaging pattern called pub/sub. Clients attach to channels to subscribe to messages, and every message published to a unique channel is broadcast by Ably to all the channel’s subscribers.

Ably’s presence APIs enable pub/sub channel clients to be aware of others currently “present” on the channel. Live presence information is handled with an event or message when a user joins the presence set and another when they leave.

Clients can attach to a channel to receive messages published on it or to publish messages. They do not necessarily need to mark themselves as present but if they want to be seen by other users, they may explicitly call enter to join the presence set, which emits an enter event to notify others of their presence.

They can retrieve the current members of the presence set for the channel by calling get and can call subscribe to receive events as the membership of the presence set changes.

Each client on a channel has a self-assigned client identifier and a system-assigned connection identifier, along with an optional payload that can describe their status or attributes. The optional payload can be a string, JSON object, JSON array, or binary data.

Ably offers the following presence events:

enter A new member has entered the presence set
leave A member who was present has now left the set.
This may be an explicit request to leave or the member may detach from the channel.
If a member’s connection is abruptly disconnected and they do not resume their connection within set time,
Ably treats this as a leave event.
update A present member has updated their member data
Being notified of member data updates can be very useful.
For example, to update when the user types a message

Presence event messages include the client ID, connection ID, and an action that tells you about the event: either enter, update, or leave.

Details of the code needed to use Ably’s realtime API for presence updates are available in an Ably tutorial; the example code is available to download from Github.

What is the difference between presence and occupancy?

Ably Presence provides information about the activity of clients who have explicitly entered into the presence set of a channel. Presence events are emitted by any client who has entered the presence set as a member (when they enter, leave, or update their member data).

Not all clients of a channel will enter the presence set, so Ably Occupancy provides metrics about all clients attached to a channel. This includes a count of the number attached, the number of publishers and subscribers, the number of presence set members, and the number of presence publishers and subscribers.

The challenge of user presence at scale

When the presence set of a channel has just a few members, the number of presence events is manageable, but consider the following example as the numbers scale.

  • A user enters the presence set of a channel in a live event system and subscribes to presence notifications. They receive a presence event. Total number of presence events: 1
  • A second user enters the presence set of the same channel and subscribes to presence notifications. Both users receive a presence event. Total number of presence events: 3
  • A third user enters and subscribes. All three users receive a presence event. Total number of presence events: 6
  • This pattern continues until 1,000 users have entered the channel. The number of presence event messages sent on the channel to notify of each user’s presence follows the n-squared pattern (1+2+3+4…+1000). Total number of presence events: 500,500
  • Later, as all 1000 users leave the channel, their departure is published.  Total number of presence events: 500,500
Diagram illustrating the number of presence notifications increases quadratically.
The number of presence notifications increases quadratically.

In summary: for just 1,000 presence-set members on a live event channel who are subscribed to presence events, the system will receive 2000 publish events as they enter, and later, leave. But to notify every member of those enter/leave events requires sending over 1 million notifications.

Consider if all 1,000 users joined over the space of 10 seconds: those notifications need to be published at a rate of 50k per second!

Monitor user presence at scale with Ably

As we’ve shown, as the number of people subscribed to a presence set increases there is quadratic growth in presence messages sent, and message volume increases significantly with relatively small numbers of users. Even with the capacity to handle such volume at scale, you could find it costly in terms of hitting cloud-computing usage limits or incurring unexpected message costs.

To prevent our customers from accidentally hitting message limits with presence messages Ably imposes a limit on the number of clients that can enter the presence set of any channel. Note that there are no limits to the number of clients that can attach or subscribe to a channel. The limits are purely on the number of clients that can call enter to become a member of the presence set.

The limits we impose are not driven by constraints within the Ably platform but are in place as a precaution for customers who want to have very large presence sets and may not realize the implications.

If you need to maintain a large presence set, we are happy to discuss increasing the maximum number of members that can enter the set. With a larger number of members, you can avoid a disproportionate presence message fanout if only a few clients subscribe to presence changes.

The presence set members can still retrieve an updated version of the presence set at regular intervals using get, sufficient for common features such as analytics, on-demand querying, or custom functionality. Use cases vary, but it is awkward to display realtime presence information for all users within a large presence set, for example, the status of 1000 users in a chatroom.

About Ably

Ably is an edge messaging platform that uses pub/sub messaging to deliver data at low latency, You get a globally-distributed scalable infrastructure out-of-the-box, which takes care of connection management, message ordering and guaranteed exactly-once delivery. Ably offers higher-level functionality to aid building rich live experiences, such as presence, and additional easy ways to integrate with third-party APIs.

Learn more about the Ably platform, get in touch or sign up for a free account to see what Ably can do for you.

Banner showing an avatar stack, typing indicator and emojis

Join the Ably newsletter today

1000s of industry pioneers trust Ably for monthly insights on the realtime data economy.
Enter your email