Counting live users at scale with subscription_count events

Subscription-Event-Count-release-1.png

Releasing the new event type in Pusher Channels: Subscription Count event! This type of event automatically provides the number of connections currently subscribed to any given channel.

Introduction

Presence at scale is tricky.

Today we’re releasing a new event type in Pusher Channels: subscription counting events. subscription_count events automatically provide the number of connections currently subscribed to any given channel, allowing you to implement presence at scale.

Presence channels have always provided awareness of who is subscribed to a channel in a Pusher app. That particular piece of information makes it easy to build “who’s online” style functionality: chat rooms, teams collaborating in real time on a document, audiences in a live video stream, users viewing the same web page, players within an online game, etc.

But what if you’re looking at hundreds or thousands of users? We limit Presence channels to a maximum of 100 members. This is because the number of presence messages distributed grows exponentially with the number of people present in the channel which can cause a real headache and eat away at your message limit pretty fast.

Simply put, the vanilla presence channel use case doesn’t suit big numbers, so we’ve built subscription counts to serve large-scale presence.

What is a Subscription Count event?

A subscription_count is an event that can be manually enabled to count the number of connections that are currently subscribed to a particular channel. They work with all channel types, except presence channels.

Subscription count UI example

Implementing presence at scale

subscription_count is useful for scenarios where you want to provide a live connection number at a large scale, and aren’t necessarily interested in who is present, joining, or leaving (which is the main use case for presence).

Previously, you would need to query the number of connections to a channel, before pushing this information to the channel to make it available to the clients.

Channels using these events aren’t held to the same limitations as presence channels, so you can show a realtime count of thousands of live users in your app. You might want to use subscription count to show:

  • audience size in a live stream
  • number of users waiting in a queue
  • how many players are live in an open world game

How do you implement Subscription Count?

subscription_count works with all channel types except Presence channels. You can turn on this feature directly in the Channels dashboard. Enable the event toggle and Pusher will publish a subscription_count event whenever the number of subscribers on a given channel changes.

channel.bind("pusher:subscription_count", (data) => {
console.log(data.subscription_count);
console.log(channel.subscription_count);
});

How does subscription counting work?

As soon as a user subscribes to a channel, a pusher_internal event is triggered with the name subscription_count containing the number of subscribers currently present in the channel.

If the number of subscribers in the channel is fewer than or equal to 100, the client will receive updated counts every time someone subscribes or unsubscribes to a channel. When the channel grows to more than 100 subscribers, the client will receive an updated count of the last 30 seconds in a single event. This protects your message count from taking a hit based on high activity.

How subscription count events work in Pusher Channels

We maintain these counts for every cluster. With multiple instances of Pusher running on each cluster, there is a global count for a channel and a local count per instance. Instances update the counts in an atomic operation after every successful subscribe and unsubscribe event.

Maintaining the counts protects user experience. In the case that an instance goes down and subscribers from that instance were lost, a cleanup script will decrease the last known count from the global count.

Why doesn’t subscription counting work with Presence channels?

To understand this, let’s first talk about how we are sending out these events.

We have divided the event emission into two parts:

  • Small groups (fewer than or equal to 100 subscribers)
  • Large groups (more than 100 subscribers)

For small groups, we publish the new count with every subscription/unsubscription. For a large group, since there’s a lot of activity, we bundle these events in a window of 30 seconds and send a single event for that window.

Pusher categorizes Presence channels as small groups. For such channels, we already broadcast member_added and member_removed, making subscription count irrelevant for this particular type of channel. So to save on message quota for the app, we have disabled the subscription count on Presence channels.

Getting Started with Subscription Count

Here’s an example for a company chat app. Chat users always want to know how many people are currently online.

Step 1 – Create your first app

  • Sign in and on the Pusher dashboard, next to Channels, click Create App. If you don’t have an account, sign up for free.
  • Name your app and choose a cluster. You can also let us know which stack you’re using to help us personalize your dashboard.
    Create a new Channel app
  • To continue, you need the App Key. Navigate to App Keys and copy your key.
    Copy your app keys for the app
  • Enable subscription count feature. Navigate to App Settings and turn on the Subscription Count Event.
    Enable the Subscription Count feature using a toggle

IMPORTANT:

To use the feature, you’ll need to enable both subscription counting and subscription count events.

Step 2 – Connect to your app

For this example, we’ve used a public channel.

let pusher = new Pusher('', {

cluster: '<region>',

});

let channel = pusher.subscribe('my-channel');

channel.bind('pusher:subscription_count', (data) => {

console.log(data.subscription_count);

});

Ready to start implementing presence features for your realtime apps? Sign in to the Pusher dashboard or create a free sandbox account to start experimenting.