# `Redis.PubSub`
[🔗](https://github.com/joshrotenberg/redis_ex/blob/v0.7.1/lib/redis/pubsub/pubsub.ex#L1)

Pub/Sub client for Redis.

Manages a dedicated pub/sub connection and routes messages to subscribing
Elixir processes as regular Erlang messages.

## Usage

    {:ok, ps} = Redis.PubSub.start_link(port: 6379)
    :ok = Redis.PubSub.subscribe(ps, "mychannel", self())

    receive do
      {:redis_pubsub, :message, channel, payload} ->
        IO.puts(channel <> ": " <> payload)
    end

    :ok = Redis.PubSub.unsubscribe(ps, "mychannel", self())

## Message Format

    {:redis_pubsub, :message, channel, payload}
    {:redis_pubsub, :pmessage, pattern, channel, payload}
    {:redis_pubsub, :subscribed, channel_or_pattern, count}
    {:redis_pubsub, :unsubscribed, channel_or_pattern, count}

## Options

  * `:host` - Redis host (default: "127.0.0.1")
  * `:port` - Redis port (default: 6379)
  * `:password` - authentication password
  * `:username` - authentication username
  * `:name` - GenServer name registration
  * `:timeout` - connection timeout ms (default: 5_000)

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `psubscribe`

```elixir
@spec psubscribe(GenServer.server(), String.t() | [String.t()], pid()) ::
  :ok | {:error, term()}
```

Subscribe `subscriber` to a pattern. Messages arrive as `{:redis_pubsub, :pmessage, pattern, channel, payload}`.

# `punsubscribe`

```elixir
@spec punsubscribe(GenServer.server(), String.t() | [String.t()], pid()) ::
  :ok | {:error, term()}
```

Unsubscribe `subscriber` from a pattern.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

# `stop`

```elixir
@spec stop(GenServer.server()) :: :ok
```

Stops the pub/sub connection.

# `subscribe`

```elixir
@spec subscribe(GenServer.server(), String.t() | [String.t()], pid()) ::
  :ok | {:error, term()}
```

Subscribe `subscriber` to `channel`. Messages arrive as `{:redis_pubsub, :message, channel, payload}`.

# `subscriptions`

```elixir
@spec subscriptions(GenServer.server()) :: map()
```

Returns current subscription state: channels and patterns with their subscriber counts.

# `unsubscribe`

```elixir
@spec unsubscribe(GenServer.server(), String.t() | [String.t()], pid()) ::
  :ok | {:error, term()}
```

Unsubscribe `subscriber` from `channel`.

---

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