Tracking Real-Time User Activity

Learn how to track user activity using Presence in this lesson.

Web applications are full of rich interactions. We can think of these interactions as an active conversation between the user and our app. In Phoenix, those conversations are represented and managed by processes—often implemented with channels. By gathering up a list of active processes, we can show the active conversations happening on our site. This is exactly the job of Phoenix Presence, a behavior that provides the capabilities to track a user’s conversation, or presence, within your application.

Issues in tracking user activity

Tracking activity on a network is an easy problem to solve when everything is on one server. However, that is rarely the case. In the real world, servers are clustered together for performance and reliability, and connections between those servers sometimes fail. These problems make tracking presence by listing processes notoriously difficult.

Phoenix Presence solves these problems for us. It is built on top of Phoenix PubSub and leverages PubSub’s distributed capabilities to reliably track processes across a distributed set of servers. It also uses a CRDT (Conflict-free Replicated Data Type) model to ensure that presence tracking will keep working when nodes or connections on our network fail.

When we’re done, our dashboard will display a section that shows which users are viewing which products at a given moment. The list will update immediately as users visit and leave a Product Show LiveView, something like this:

Get hands-on with 1200+ tech skills courses.