PubSub v1.1.1 PubSub View Source

Publish-Subscribe utility process. Use start_link/0 to start it:

PubSub.start_link()

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Delivers a message to the given topic.

Starts the server.

Subscribes a process to the given topic.

Returns a list of pids representing the processes that are currently subscribed to the given topic.

Returns a list of the current topics.

Unsubscribes a process from a given topic.

Link to this section Types

Specs

message() :: any()

Specs

topic() :: atom()

Link to this section Functions

Specs

child_spec(list()) :: map()

Returns a specification to start this module under a supervisor.

See Supervisor.

Callback implementation for GenServer.init/1.

Specs

publish(topic(), message()) :: :ok

Delivers a message to the given topic.

Example

iex> PubSub.publish(:my_topic, "Hi there!")
:ok

Specs

start_link(list()) :: GenServer.on_start()

Starts the server.

Specs

subscribe(pid(), topic()) :: :ok

Subscribes a process to the given topic.

Example

iex> pid = self()
iex> PubSub.subscribe(pid, :my_topic)
:ok

Specs

subscribers(topic()) :: [pid()]

Returns a list of pids representing the processes that are currently subscribed to the given topic.

Example

iex> pid = self()
iex> PubSub.subscribe(pid, :my_topic)
iex> [subscriber] = PubSub.subscribers(:my_topic)
iex> subscriber == pid
true

Specs

topics() :: [topic()]

Returns a list of the current topics.

Example

iex> pid = self()
iex> PubSub.subscribe(pid, :my_topic)
iex> PubSub.subscribe(pid, :your_topic)
iex> PubSub.topics
[:my_topic, :your_topic]

Specs

unsubscribe(pid(), topic()) :: :ok

Unsubscribes a process from a given topic.

Example

iex> pid = self()
iex> PubSub.unsubscribe(pid, :my_topic)
:ok