Phoenix Presence

Understand the details of Phoenix's Presence in the game.

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 could disrupt communication to the database, and the ...