View Source PubSub (PubSub v1.1.3)

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

PubSub.start_link()

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.

Types

message()

@type message() :: any()

topic()

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

Functions

child_spec(init_arg)

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

Returns a specification to start this module under a supervisor.

See Supervisor.

init(args)

Callback implementation for GenServer.init/1.

publish(topic, message)

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

Delivers a message to the given topic.

Example

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

start_link(args \\ [])

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

Starts the server.

subscribe(pid, topic)

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

Subscribes a process to the given topic.

Example

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

subscribers(topic)

@spec 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

topics()

@spec 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]

unsubscribe(pid, topic)

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

Unsubscribes a process from a given topic.

Example

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