pg2pubsub v0.1.7 Pg2PubSub
Provides methods for subscribing and publishing to named topics.
Summary
Functions
Publish to a topic
Starts a PubSub process linked to the calling process
Subscribe to a topic
Unsubscribe from a topic
Functions
Publish to a topic
Parameters
- pid: Process ID for the started PubSub process
- topic: Name of the topic to unsubscribe from
Examples
iex> {:ok, pid} = Pg2PubSub.start_link
iex> Pg2PubSub.subscribe(pid, "foo")
:ok
iex> Pg2PubSub.publish(pid, "foo", "bar")
:ok
iex> receive do msg -> msg end
"bar"
Specs
start_link :: GenServer.on_start
Starts a PubSub process linked to the calling process
Examples
iex> {:ok, pid} = Pg2PubSub.start_link
iex> is_pid(pid)
true
Specs
subscribe(pid, String.t) :: term
Subscribe to a topic
Parameters
- pid: Process ID for the started PubSub process
- topic: Name of the topic to subscribe to
Examples
iex> {:ok, pid} = Pg2PubSub.start_link
iex> Pg2PubSub.subscribe(pid, "foo")
:ok
# subscribing a second time has no effect
iex> {:ok, pid} = Pg2PubSub.start_link
iex> Pg2PubSub.subscribe(pid, "foo")
:ok
iex> Pg2PubSub.subscribe(pid, "foo")
{:already_registered, [self]}
Specs
unsubscribe(pid, String.t) :: term
Unsubscribe from a topic
Parameters
- pid: Process ID for the started PubSub process
- topic: Name of the topic to unsubscribe from
Examples
iex> {:ok, pid} = Pg2PubSub.start_link
iex> Pg2PubSub.subscribe(pid, "foo")
:ok
iex> Pg2PubSub.unsubscribe(pid, "foo")
:ok
# unsubscribing when not subscribed will still give an okay result
iex> {:ok, pid} = Pg2PubSub.start_link
iex> Pg2PubSub.unsubscribe(pid, "foo")
:ok