Phoenix Presence
Explore how Phoenix Presence manages tracking of clients in real-time channels using conflict-free replicated data types. Understand the setup, usage, and benefits of Presence for monitoring subscribers in a distributed Elixir application.
We'll cover the following...
What is Phoenix Presence?
From the earliest days of Phoenix channels, developers have tried to figure out how to tell who is currently subscribed to a Channel. Until recently, the answer was to create a custom solution that best fits an individual application’s circumstances. However, we now have Phoenix Presence to solve that problem in a general way for all of us.
Presence has one job to do: to keep track of the clients subscribed to a topic on a Channel. For us, this means keeping track of the players in each game. Presence does this amazingly. This might sound like a trivial task, but it’s deceptively difficult.
If we were to roll our own version of Presence, our first thought might be to maintain a list of the subscribers, add clients to the list when they join, and remove them when they leave. This might work for a system with a single node.
With a single data structure on multiple nodes, though, we would have to make sure that the data is available to all nodes in the cluster. However, nodes don’t stay up forever, and a crash could lose all the subscription data.
We could put the data in an external database to solve that data durability problem. However, then network hiccups ...