View Source PubSub (PubSub v1.1.2)

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

@type message() :: any()
@type topic() :: atom() | binary()

Link to this section Functions

@spec child_spec(list()) :: map()

Returns a specification to start this module under a supervisor.

See Supervisor.

Callback implementation for GenServer.init/1.

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

Delivers a message to the given topic.

example

Example

iex> PubSub.publish(:my_topic, "Hi there!")
:ok
@spec start_link(list()) :: GenServer.on_start()

Starts the server.

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

Subscribes a process to the given topic.

example

Example

iex> pid = self()
iex> PubSub.subscribe(pid, :my_topic)
:ok
@spec subscribers(topic()) :: [pid()]

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

example

Example

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

Returns a list of the current topics.

example

Example

iex> pid = self()
iex> PubSub.subscribe(pid, :my_topic)
iex> PubSub.subscribe(pid, :your_topic)
iex> PubSub.topics
[:my_topic, :your_topic]
@spec unsubscribe(pid(), topic()) :: :ok

Unsubscribes a process from a given topic.

example

Example

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