Add configurable default dispatcher#209
Merged
josevalim merged 1 commit intophoenixframework:mainfrom Apr 17, 2026
Merged
Conversation
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.
Member
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
:dispatcheroption tochild_spec/start_linkthat sets the default dispatcher module for all broadcast functions. Defaults toPhoenix.PubSub(preserving existing behavior). Can be overridden per-call by passing a dispatcher tobroadcast/4and friends.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:
broadcastcallBoth add boilerplate for what is conceptually a per-PubSub configuration. The wrapper approach is particularly fragile. Any code that calls
Phoenix.PubSub.broadcast/3directly 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
:pubsubtuple is extended from{adapter, name}to{adapter, name, dispatcher}. SingleRegistry.metalookup per broadcast call, same as before. No additional lookups.Changes
Phoenix.PubSub.Supervisoraccepts:dispatcheroption, stores it in the meta tuplenil, read the configured default from the meta tuple, and fall back to it when no explicit dispatcher is passed