phoenix-pubsub-patterns

Installation
SKILL.md

Phoenix PubSub Patterns

RULES — Follow these with no exceptions

  1. Always guard subscriptions with if connected?(socket) — prevents duplicate subscriptions on static render (LiveView mounts twice: once static, once connected)
  2. Broadcast from contexts, not LiveViews — keeps real-time logic in the business layer; LiveViews only subscribe and react
  3. Use consistent topic naming"resource:id" for specific resources, "resource:action" for collection-wide events
  4. Handle PubSub messages in handle_info/2 — never in handle_event/3; PubSub messages are process messages, not client events
  5. Update assigns immutably with update/3 — never replace the full list; use update(socket, :items, &[new | &1])
  6. Test PubSub by calling context functions and asserting LiveView updates — don't test PubSub.broadcast directly; test the full cycle

Subscription Pattern

Subscribe in mount/3 only when connected. The static render doesn't need real-time updates.

Installs
2
GitHub Stars
134
First Seen
Apr 21, 2026
phoenix-pubsub-patterns — j-morgan6/elixir-phoenix-guide