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(pid, topic, msg)

Specs

publish(pid, String.t, String.t) :: :ok

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"
start_link()

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
subscribe(pid, topic)

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]}
unsubscribe(pid, topic)

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