Skip to content

Add configurable default dispatcher#209

Merged
josevalim merged 1 commit intophoenixframework:mainfrom
twinn:feature/default-dispatcher
Apr 17, 2026
Merged

Add configurable default dispatcher#209
josevalim merged 1 commit intophoenixframework:mainfrom
twinn:feature/default-dispatcher

Conversation

@twinn
Copy link
Copy Markdown
Contributor

@twinn twinn commented Apr 15, 2026

Summary

Adds a :dispatcher option to child_spec/start_link that sets the default dispatcher module for all broadcast functions. Defaults to Phoenix.PubSub (preserving existing behavior). Can be overridden per-call by passing a dispatcher to broadcast/4 and friends.

{Phoenix.PubSub, name: :my_pubsub, dispatcher: MyApp.Dispatcher}

Motivation

The custom dispatching feature is powerful, but in practice it's difficult to use as a default for an application. The current options are:

  1. Pass the dispatcher explicitly on every broadcast call
  2. Create a wrapper module that injects the dispatcher

Both add boilerplate for what is conceptually a per-PubSub configuration. The wrapper approach is particularly fragile. Any code that calls Phoenix.PubSub.broadcast/3 directly instead of going through the wrapper bypasses the custom dispatcher entirely. In a growing codebase with multiple contributors, this is easy to miss.

The use case I ran into was filtering broadcasts in test environments, scoping delivery so that concurrent async tests sharing a topic don't leak messages to each other. A configurable default makes this a one-line change at startup rather than a module-level abstraction that every callsite must remember to use.

Implementation

The internal meta :pubsub tuple is extended from {adapter, name} to {adapter, name, dispatcher}. Single Registry.meta lookup per broadcast call, same as before. No additional lookups.

Changes

  • Phoenix.PubSub.Supervisor accepts :dispatcher option, stores it in the meta tuple
  • All broadcast functions default dispatcher to nil, read the configured default from the meta tuple, and fall back to it when no explicit dispatcher is passed
  • 6 new tests covering default dispatcher, per-call override, and all broadcast variants

Adds a :dispatcher option to child_spec/start_link that sets the
default dispatcher module for all broadcast functions. Defaults to
Phoenix.PubSub (preserving existing behavior). Can be overridden
per-call by passing a dispatcher to broadcast/4 and friends.

    {Phoenix.PubSub, name: :my_pubsub, dispatcher: MyApp.Dispatcher}

The internal meta :pubsub value is extended from {adapter, name}
to {adapter, name, dispatcher}. Single Registry.meta lookup per
broadcast call, same as before.
@josevalim josevalim merged commit 228b82e into phoenixframework:main Apr 17, 2026
5 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants