Everything you need to know about Pusher Presence channels and how to use them to build a live “who’s online” feature.
Pusher Channels has five channel types you can use to build the realtime messaging infrastructure for your application.
Presence channels provide your end users with a list of channel occupants. Existing subscribers are notified when new users subscribe or unsubscribe from the channel. Simply put, it allows you to build a live “who’s online” feature.
We built Presence channels on top of private channels which allows you to associate user IDs with channel subscriptions. This way only authorized users can subscribe to your Presence channel as an extra layer of security.
Presence channels are ideal for small groups of subscribers. For example, when building different chat apps and live chat options, online multiplayer games, or tracking updates and changes when collaborating on an online document.
Presence channels work similarly to private channels. They require authorization and must have a prefix presence-
.
When building with presence channels, you can set up your app so users can see the following:
Users can subscribe and unsubscribe. When a user subscribes to a presence channel, the system triggers the authorization process.
var presenceChannel = pusher.subscribe(presenceChannelName);
The user object (user_id
and user_info
) is processed as part of the authorization callback and becomes available to event listeners in your application. User information is then shared with other subscribers in this channel so they can see who has joined and who’s online. You can include any additional user information here, like different social media accounts, nicknames, or roles. Go to Authenticating users for more details.
Additionally, subscribers can subscribe to presence events. This sends an update to the client whenever someone joins or leaves the channel. To set this up, go to Events.
If you want to see which users are subscribed to Presence channels, you can query the application state.
Here’s a sequence diagram to show you how all this works.
The presence channels use case is not limited to chat apps. You can use it to implement presence in collaborative tools, activity feeds, online gaming, social media, and online streaming apps.
Let’s take this scenario as an example:
Multiple team members are collaborating on an online document. Any changes made by one subscriber will be pushed, in real time, to other subscribers in this channel.
Compared to public channels, the added value of private and/or presence channels in this example is limiting who has access and editing permissions to make changes. Also, because through authentication we know who the user is, presence channels can provide information on who committed which changes and when.
For tips on how to implement mouse tracking, take a look at When to trigger events.
Additionally, typing indicators would make a useful addition to this app. To do this, we recommend you use client events. These events originate at the client and are broadcast to other subscribers of the channel.
End-to-end implementation guides are available for the following SDKs:
How does using Channels as infrastructure solve presence complexity?
The benefit of using Pusher Channels is that you don’t have to build and maintain your own realtime experience infrastructure.
Building presence from scratch is a complex undertaking.
Implementing presence requires the following:
The great thing about Channels as infrastructure is that you can combine presence channels with other powerful realtime capabilities. Rather than building multiple realtime features individually, you can take advantage of the full package of Channels features to include a full realtime experience in your app.
If you don’t want to use presence channels, you can still use webhooks to know when subscribers are online and if needed, change your app’s logic accordingly. For example, you may want to add a rule that when subscribers are online, they won’t receive email notifications about their account but will receive an in-app alert via a Channels event instead.
Another example might be for ride-sharing apps: you can configure your application not to send ride requests to drivers who are currently offline.
Presence channels cover a wide range of use cases, however, they’re currently limited to channels with fewer than 100 members.
If you plan on creating a channel with more than 100 members, use a different type of channel (for example Private channel) and Subscription Count events instead of Presence channels.
The Subscription Count events will retrieve the total number of members in your 100+ members channel. Here’s Subscription Count Event explained.
To learn more about scaling user presence, take a look at our guide on Using presence for large groups. If you want to learn about how we tackle large-scale presence, check out this read from our engineering journal, How the Pusher team built subscription counting at scale.
From the Pusher dashboard, you can automatically track the number of peak concurrent connections and the number of sent messages. To find out more, read through channel metrics.
Additionally, you can use the members object in the client and member_added/removed webhooks to track the number of online users in the channel using. Visit Presence events docs for more details. Start building
To get started, sign up for free. With the free sandbox plan, you get 100 concurrent connections and 200,000 messages daily. Find more details about Pusher Channels pricing.
Once you have an account, create your app. Follow the steps directly from the dashboard UI, Getting Started guide.
For further details and specific programming language references, take a look at our comprehensive Channel documentation.
To start building presence with rich user information, follow the instructions in our Build an app with information-rich “Who’s online” feature blog post.
If you’re looking for tutorials on how to build stuff using presence, here are a few that you might want to consider: