Phoenix.Topic
Serves as a Notification and PubSub layer for broad use-cases. Used internally by Channels for pubsub broadcast.
Example
iex> Topic.subscribe(self, "user:123")
:ok
iex> Process.info(self)[:messages]
[]
iex> Topic.subscribers("user:123")
[#PID<0.169.0>]
iex> Topic.broadcast "user:123", {:user_update, %{id: 123, name: "Shane"}}
:ok
iex> Process.info(self)[:messages]
{:user_update, %{id: 123, name: "Shane"}}
Summary
| active?(name) | Check if Topic is active. To be active it must be created and have subscribers |
| broadcast(name, message) | Broadcasts a message to the Topic’s process group subscribers |
| broadcast_from(from_pid, name, message) | Broadcasts a message to the Topic’s process group subscribers, excluding broadcaster from receiving the message it sent out |
| create(name) | Creates a Topic for pubsub broadcast to subscribers |
| delete(name) | Removes Topic from process group if inactive |
| exists?(name) | Checks if a given Topic is registered as a process group |
| list() | Returns a List of all Phoenix Topics from :pg2 |
| subscribe(pid, name) | Adds subsriber pid to given Topic process group |
| subscribers(name) | Returns the List of subsriber pids of the Topic’s process group |
| unsubscribe(pid, name) | Removes the given subscriber from the Topic’s process group |
Functions
Check if Topic is active. To be active it must be created and have subscribers
Broadcasts a message to the Topic’s process group subscribers
Examples
iex> Topic.broadcast("mytopic", :hello)
To exclude the broadcaster from receiving the message, use broadcast_from/3
Broadcasts a message to the Topic’s process group subscribers, excluding broadcaster from receiving the message it sent out
Examples
iex> Topic.broadcast_from(self, "mytopic", :hello)
Creates a Topic for pubsub broadcast to subscribers
- name - The String name of the topic
Examples
iex> Topic.create("mytopic")
:ok
Removes Topic from process group if inactive
Examples
iex> Topic.delete("mytopic")
:ok
iex> Topic.delete("activetopic")
{:error, :active}
Adds subsriber pid to given Topic process group
Examples
iex> Topic.subscribe(self, "mytopic")
Returns the List of subsriber pids of the Topic’s process group
Examples
iex> Topic.subscribers("mytopic")
[]
iex> Topic.subscribe(self, "mytopic")
:ok
iex> Topic.subscribers("mytopic")
[#PID<0.41.0>]