PubSubx (pub_subx v0.1.2)

PubSubx is a simple publish-subscribe (PubSub) system built on top of Elixir's GenServer and Registry.

The module allows processes to subscribe to topics, publish messages to those topics, and manage subscriptions. It efficiently handles message delivery to subscribed processes and automatically cleans up subscriptions when processes terminate.

Features

  • Subscribe/Unsubscribe: Processes can subscribe or unsubscribe from topics.
  • Publish: Messages can be published to a topic, and all subscribers to that topic will receive the message.
  • Dynamic Topics: Topics are dynamically created as they are subscribed to, and they are removed when no subscribers exist.
  • Process Monitoring: Automatically removes subscribers when the process is no longer alive.

Example Usage

Start the PubSubx server:

{:ok, pid} = PubSubx.start_link(name: :my_pubsub)

Subscribe a process to a topic:

PubSubx.subscribe(:my_topic, self(), :my_pubsub)

Publish a message to the topic:

PubSubx.publish(:my_topic, "Hello, subscribers!", :my_pubsub)

Get the list of subscribers:

subscribers = PubSubx.subscribers(:my_topic, :my_pubsub)

Unsubscribe a process from a topic:

PubSubx.unsubscribe(:my_topic, self(), :my_pubsub)

Summary

Functions

Returns a specification to start this module under a supervisor.

Publishes a message to the specified topic.

Starts the PubSubx server.

Subscribes a given process (pid) to a specific topic.

Returns a list of PIDs that are subscribed to the specified topic.

Lists all topics that have active subscribers.

Unsubscribes a given process (pid) from the specified topic.

Types

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

Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

publish(topic, message, name \\ __MODULE__)

@spec publish(topic(), term(), process()) :: :ok

Publishes a message to the specified topic.

All subscribers to that topic will receive the message.

If a name is not provided, it defaults to the PubSubx module name.

Link to this function

start_link(opts \\ [])

@spec start_link(Keyword.t()) :: GenServer.on_start()

Starts the PubSubx server.

Options

Link to this function

subscribe(topic, pid, name \\ __MODULE__)

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

Subscribes a given process (pid) to a specific topic.

If a name is not provided, it defaults to the PubSubx module name.

Link to this function

subscribers(topic, name \\ __MODULE__)

@spec subscribers(topic(), process()) :: [pid()]

Returns a list of PIDs that are subscribed to the specified topic.

If a name is not provided, it defaults to the PubSubx module name.

Link to this function

topics(name \\ __MODULE__)

@spec topics(process()) :: [topic()]

Lists all topics that have active subscribers.

If a name is not provided, it defaults to the PubSubx module name.

Link to this function

unsubscribe(topic, pid, name \\ __MODULE__)

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

Unsubscribes a given process (pid) from the specified topic.

If a name is not provided, it defaults to the PubSubx module name.