📎 Webclip
Distributed Phoenix Chat with PubSub PG2 adapter
The article shows how to cluster Phoenix Chat nodes with the BEAM communication protocol, then uses pg2 to group processes across nodes and inspect how Phoenix broadcasts chat messages in a distributed app. It contrasts this approach with Redis PubSub, which is easy to set up but introduces a single point of failure and extra server maintenance.
Reading notes#
- The BEAM lets Elixir nodes connect directly, so one node can talk to remote processes on another node after
Node.connect/1. pg2is an Erlang module for process grouping across multiple nodes.- A
pg2group keeps track of member PIDs and monitors them locally. - When a process exits,
pg2removes it from the group. pg2does not provide a direct broadcast function, so messages are sent by enumerating group members one by one.- In Phoenix, the
Phoenix.PubSub.PG2adapter is configured by default in a new app. - Each Phoenix node starts a local
PubSub.PG2Serverand registers it in the{:phx, Chat.PubSub}group. - The group members are the
PubSub.PG2ServerPIDs, not the user socket processes. - A message is broadcast locally first, then forwarded once to the remote
PubSub.PG2Server, which rebroadcasts it to browsers on that node. - The article ends by noting that manual node connection is a problem for production, and points to libcluster for automatic clustering.
