Phoenix.PubSub

Serves as a Notification and PubSub layer for broad use-cases. Used internally by Channels for pubsub broadcast.

Example

iex> PubSub.subscribe(self, "user:123")
:ok
iex> Process.info(self)[:messages]
[]
iex> PubSub.subscribers("user:123")
[#PID<0.169.0>]
iex> PubSub.broadcast "user:123", {:user_update, %{id: 123, name: "Shane"}}
:ok
iex> Process.info(self)[:messages]
{:user_update, %{id: 123, name: "Shane"}}

Summary

active?(topic_name)

Check if PubSub is active. To be active it must be created and have subscribers

broadcast(topic_name, message)

Broadcasts a message to the topic’s subscribers

broadcast_from(from_pid, topic_name, message)

Broadcasts a message to the topics’s subscribers, excluding broadcaster from receiving the message it sent out

create(topic_name)

Creates a topic for pubsub broadcast to subscribers

delete(topic_name)

Removes topic from process group if inactive

exists?(topic_name)

Checks if a given topic is registered as a process group

list()

Returns a List of all Phoenix PubSubs from :pg2

subscribe(pid, topic_name)

Adds subsriber pid to the given topic

subscribers(topic_name)

Returns the List of subsriber pids for the give topic

unsubscribe(pid, topic_name)

Removes the given subscriber from the topic

Functions

active?(topic_name)

Check if PubSub is active. To be active it must be created and have subscribers

broadcast(topic_name, message)

Broadcasts a message to the topic’s subscribers

  • topic_name - The String name of the topic
  • message - The term to broadcast

Examples

iex> PubSub.broadcast("mytopic", :hello)

To exclude the broadcaster from receiving the message, use broadcast_from/3

broadcast_from(from_pid, topic_name, message)

Broadcasts a message to the topics’s subscribers, excluding broadcaster from receiving the message it sent out

  • topic_name - The String name of the topic
  • message - The term to broadcast

Examples

iex> PubSub.broadcast_from(self, "mytopic", :hello)
create(topic_name)

Creates a topic for pubsub broadcast to subscribers

  • topic_name - The String name of the topic

Examples

iex> PubSub.create("mytopic")
:ok
delete(topic_name)

Removes topic from process group if inactive

Examples

iex> PubSub.delete("mytopic")
:ok
iex> PubSub.delete("activetopic")
{:error, :active}
exists?(topic_name)

Checks if a given topic is registered as a process group

list()

Returns a List of all Phoenix PubSubs from :pg2

subscribe(pid, topic_name)

Adds subsriber pid to the given topic

  • pid - The Pid of the subscriber
  • topic_name - The String name of the topic

Examples

iex> PubSub.subscribe(self, "mytopic")
subscribers(topic_name)

Returns the List of subsriber pids for the give topic

Examples

iex> PubSub.subscribers("mytopic")
[]
iex> PubSub.subscribe(self, "mytopic")
:ok
iex> PubSub.subscribers("mytopic")
[#PID<0.41.0>]
unsubscribe(pid, topic_name)

Removes the given subscriber from the topic

  • pid - The Pid of the subscriber
  • topic_name - The String name of the topic

Examples

iex> PubSub.unsubscribe(self, "mytopic")