Showing who's online with a large number of users

A common question we have is: ‘how do we detect who’s online/offline with a number of users well beyond the presence channel limit of 100?’.

There are two solutions to this. Both of which are outlined below:

1. Use a unique Channel per User to know exactly who is online

Here are the steps you can take:

  1. On, set up a ‘Channel Existence’ WebHook, pointing to an endpoint on your servers. Click here for more information on WebHooks.

  2. Assuming the user is logged in, pass the ID of the user from the server to the client. You can do this through many ways. One such way is to dynamically set a JS variable using embedded server-side code. To use a PHP example: var userId = '<?php echo( $user->id ); ?>';. Another such way is to render the ID in an HTML tag attribute, then use Javascript to get that tag. Alternatively you can store the ID in a cookie, and extract it with your client-side code.

  3. Have your client subscribe to a channel called 'private-' + user_id. E.g. 'private-1234'.

  4. When the user joins or leaves the channel, the channel_occupied or channel_vacated event will be fired, and the webhook will send a payload to the specified endpoint on your server.

  5. At that endpoint, do whatever it is you were aiming to do with this online/offline information. For example, save the attribute in your database to true or false accordingly. Or you could fire another Pusher event to the view where you wish to show who is online or offline.

2. Use a global Channel and keep track of unique Subscribers

A simpler, but less “real-time” solution is to periodically check the number of subscribers to a channel that all the users of an application subscribe to. The interval that you decide to check for changes determines how up to date the value displayed within your application will be.

The steps for this solution are:

  1. Tick "Enable subscription counting" in the App Settings tab in the dashboard. This count is the unique number of subscribers to the channel.

  2. Have a channel within your application that all users subscribe to e.g. global-channel

  3. At a defined interval query the Pusher HTTP API to get the subscription_count for the global-channel.

    Pusher.get('/channels/global-channel', { 'info' => 'subscription_count' })

    The response will look as follows:

      occupied: true,
      subscription_count: 42
  4. You can then publish the current subscription count on the global-channel.
    Pusher.trigger('global-channel', 'online-user-count', subscription_count)
Was this article helpful?
7 out of 10 found this helpful
Haven't found what you were looking for?
Submit a ticket