Dala.Platform.PubSub (dala v0.3.2)

Copy Markdown View Source

Simplified PubSub for Dala apps using Elixir's built-in Registry.

No Redis, no adapters — just fast local pubsub for screens to communicate.

Usage

# In your app's supervision tree:
children = [
  {Dala.PubSub, name: MyApp.PubSub}
]

# Subscribe to topics:
Dala.PubSub.subscribe(MyApp.PubSub, "user:123")

# Broadcast messages:
Dala.PubSub.broadcast(MyApp.PubSub, "user:123", {:update, %{id: 123}})

Summary

Functions

Broadcasts a message to all subscribers of a topic.

Broadcasts a message to all subscribers except the sender.

Returns a child specification for pubsub with the given options.

Starts a PubSub instance.

Subscribes the caller to a topic.

Returns the number of subscribers for a topic.

Returns all topics with at least one subscriber.

Unsubscribes the caller from a topic.

Types

message()

@type message() :: term()

t()

@type t() :: atom()

topic()

@type topic() :: binary()

Functions

broadcast(pubsub, topic, message)

@spec broadcast(t(), topic(), message()) :: :ok

Broadcasts a message to all subscribers of a topic.

Dala.PubSub.broadcast(MyApp.PubSub, "user:123", {:update, data})

broadcast_from(pubsub, from, topic, message)

@spec broadcast_from(t(), pid(), topic(), message()) :: :ok

Broadcasts a message to all subscribers except the sender.

Dala.PubSub.broadcast_from(MyApp.PubSub, self(), "user:123", {:update, data})

child_spec(options)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns a child specification for pubsub with the given options.

Required option:

  • :name - the name of the pubsub instance

start_link(options)

@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}

Starts a PubSub instance.

Options

  • :name - the name of the pubsub instance (required)

subscribe(pubsub, topic)

@spec subscribe(t(), topic()) :: :ok | {:error, term()}

Subscribes the caller to a topic.

Dala.PubSub.subscribe(MyApp.PubSub, "user:123")

subscriber_count(pubsub, topic)

@spec subscriber_count(t(), topic()) :: non_neg_integer()

Returns the number of subscribers for a topic.

topics(pubsub)

@spec topics(t()) :: [topic()]

Returns all topics with at least one subscriber.

unsubscribe(pubsub, topic)

@spec unsubscribe(t(), topic()) :: :ok

Unsubscribes the caller from a topic.

Dala.PubSub.unsubscribe(MyApp.PubSub, "user:123")