PubSub vs Tracker vs Presence

Hello,

What’s the difference between PubSub, Tracker, and Presence? Are there valid use-cases for all three side-by-side? Would it be accurate to say that Presence is an abstraction over a shared Tracker+PubSub implementation?

I read through the documentation and some of the source code but I still feel a little unsure.

Another piece that can help me understand: What use-case is there for Presence which cannot be handled by Pubsub?

Thanks.

PubSub: allow processes to communicate with each other between a node or multiple nodes. This is important in a distributed world, where server A has a message for a user that’s connected to server B. The message is dispatched over PubSub and the process on server B picks it up for delivery to the user.

Tracker: Tracker is a CRDT-based library to keep a list of information in-sync across a cluster. This information could really be anything, although it’s commonly something related to connected users or other ephemeral types of information. When you write an implementation for Tracker, you have to provide some callbacks like handle_diff.

Presence: Implementation of Tracker that works out of the box with Phoenix Channels. You don’t really have to do anything for this other than tie your channels to a Presence module—it’s all built for you already. There is also some JS provided by Phoenix framework for handling Presence on the frontend as well.

Presence is really well suited when you want the list of presence information to be sent to your clients. The quintessential example of this is “who’s online in this chat room” type of information. If you don’t want to broadcast the presence information to clients, then Tracker may be better because you will avoid doing excess work (costs CPU) that you don’t need to do.

PubSub is really separate here. It enables communication, but it doesn’t do anything regarding keeping stateful information distributed in your cluster. It’s not as easy as “broadcast a message that someone joined the channel” because distributed systems introduce a whole host of problems (what happens if a server disconnects, there is a networking issue, etc). Tracker (and therefore Presence) is designed to work in these real-world conditions.

34 Likes

Amazing explanation. Instant bookmark!

1 Like

Awesome answer. Great implicit sales pitch for the book, too. I’ll have to check it out. Thanks!

1 Like

Indeed. Book link for posterity :slight_smile:

9 Likes