# `Dala.Platform.PubSub`
[🔗](https://github.com/manhvu/dala/blob/main/lib/dala/platform/pub_sub.ex#L1)

Simplified PubSub for Dala apps using Elixir's built-in Registry.

No Redis, no adapters — just fast local pubsub for screens to communicate.

## Usage

    # In your app's supervision tree:
    children = [
      {Dala.PubSub, name: MyApp.PubSub}
    ]

    # Subscribe to topics:
    Dala.PubSub.subscribe(MyApp.PubSub, "user:123")

    # Broadcast messages:
    Dala.PubSub.broadcast(MyApp.PubSub, "user:123", {:update, %{id: 123}})

# `message`

```elixir
@type message() :: term()
```

# `t`

```elixir
@type t() :: atom()
```

# `topic`

```elixir
@type topic() :: binary()
```

# `broadcast`

```elixir
@spec broadcast(t(), topic(), message()) :: :ok
```

Broadcasts a message to all subscribers of a topic.

    Dala.PubSub.broadcast(MyApp.PubSub, "user:123", {:update, data})

# `broadcast_from`

```elixir
@spec broadcast_from(t(), pid(), topic(), message()) :: :ok
```

Broadcasts a message to all subscribers except the sender.

    Dala.PubSub.broadcast_from(MyApp.PubSub, self(), "user:123", {:update, data})

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Returns a child specification for pubsub with the given `options`.

Required option:
  * `:name` - the name of the pubsub instance

# `start_link`

```elixir
@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}
```

Starts a PubSub instance.

## Options

  * `:name` - the name of the pubsub instance (required)

# `subscribe`

```elixir
@spec subscribe(t(), topic()) :: :ok | {:error, term()}
```

Subscribes the caller to a topic.

    Dala.PubSub.subscribe(MyApp.PubSub, "user:123")

# `subscriber_count`

```elixir
@spec subscriber_count(t(), topic()) :: non_neg_integer()
```

Returns the number of subscribers for a topic.

# `topics`

```elixir
@spec topics(t()) :: [topic()]
```

Returns all topics with at least one subscriber.

# `unsubscribe`

```elixir
@spec unsubscribe(t(), topic()) :: :ok
```

Unsubscribes the caller from a topic.

    Dala.PubSub.unsubscribe(MyApp.PubSub, "user:123")

---

*Consult [api-reference.md](api-reference.md) for complete listing*
