Packages
commanded
1.4.1
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/pubsub.ex
defmodule Commanded.PubSub do
@moduledoc """
Use the pubsub configured for a Commanded application.
"""
alias Commanded.Application
@type application :: Commanded.Application.t()
@type config :: Keyword.t() | atom
@doc """
Subscribes the caller to the PubSub adapter's topic.
"""
def subscribe(application, topic) do
{adapter, adapter_meta} = Application.pubsub_adapter(application)
adapter.subscribe(adapter_meta, topic)
end
@doc """
Broadcasts message on given topic.
* `topic` - The topic to broadcast to, ie: `"users:123"`
* `message` - The payload of the broadcast
"""
def broadcast(application, topic, message) do
{adapter, adapter_meta} = Application.pubsub_adapter(application)
adapter.broadcast(adapter_meta, topic, message)
end
@doc """
Get the configured pub/sub adapter.
Defaults to a local pub/sub, restricted to running on a single node.
"""
@spec adapter(application, config) :: {module, config}
def adapter(application, config) do
case config do
:local ->
{Commanded.PubSub.LocalPubSub, []}
adapter when is_atom(adapter) ->
{adapter, []}
config ->
if Keyword.keyword?(config) do
case Keyword.get(config, :phoenix_pubsub) do
nil ->
raise ArgumentError,
"invalid Phoenix pubsub configuration #{inspect(config)} for application " <>
inspect(application)
phoenix_pubsub_config ->
{Commanded.PubSub.PhoenixPubSub, phoenix_pubsub_config}
end
else
raise ArgumentError,
"invalid pubsub configured for application " <>
inspect(application) <> " as: " <> inspect(config)
end
end
end
end