What are Presence channels? Pusher Presence channels: when and how to use them

pusher-presence-channels-explained-presence-indicators-typing-indicators.png

Everything you need to know about Pusher Presence channels and how to use them to build a live “who’s online” feature.

Introduction

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-explained-high-level-overview-1536x864.png

How do presence channels work?

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:

  • Who has joined or left – this is a useful feature for user experience. You may want your end users to know who recently joined the channel. This is especially helpful in live chat scenarios.
  • Who is online (member status) or offline – lets users know who’s available to them at any given time. For more information, refer to Accessing channel members.
  • The number of channel members – helpful to know how many people you’re reaching in this particular channel.

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.

presence-channels-explained-sequence-diagram-1536x864.png

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.

Presence channels allows you to build who's online features with presence and typing indicators.

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:

Presence complexity and Pusher Channels infrastructure

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:

  • Long-lived WebSocket connections. Read Build vs buy: Creating realtime network infrastructure for your apps and WebSockets from scratch. To successfully manage presence, you need reliable WebSocket connections that automatically reconnect in the event of failure. If you don’t set this up, presence isn’t efficient as users drop off all the time.
  • Information about channel subscribers. Knowing that there is a new subscription is one thing, but knowing who is subscribed is another.
  • Maintaining a state of who is online at any time. It’s not just about broadcasting the change of state. New subscribers also need to know who is online when they subscribe. Take a look at Accessing channel members with members_object in Channels docs.
  • A mechanism for broadcasting events when presence state changes. The challenges you might have here are scalability and the updating stored state. Can your publishing server handle a sudden increase in load?

Combining presence channels with other Channels capabilities

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.

When not to use presence channels

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.

pusher-account-usage-overview-.png

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.

pusher-get-started-.png

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: