redix_pubsub

Elixir library for using Redis Pub/Sub features (built on top of Redix)


Keywords
elixir, redis
License
MIT

Documentation

Redix PubSub

Build Status

Elixir library for Redis Pub/Sub (based on Redix).

For basic Redis-related functionality, use Redix.

Installation

Add the :redix_pubsub dependency to your mix.exs file:

defp deps() do
  [{:redix_pubsub, ">= 0.0.0"}]
end

Use mix hex.info redix_pubsub to find out what the latest version is. Then, run mix deps.get in your shell to fetch the new dependency. Note that this library requires Erlang 20+.

Usage

A Redix.PubSub process holds a connection to Redis and acts as a pub/sub intermediary between the Redis server and Elixir processes. The architecture looks like this:

Redix.PubSub architecture

Each Redix.PubSub process is able to subcribe to or unsubscribe from multiple Redis channels. One Redix.PubSub connection can handle multiple Elixir processes subscribing each to different channels.

A Redix.PubSub process can be started via Redix.PubSub.start_link/2:

{:ok, pubsub} = Redix.PubSub.start_link()

Most communication with the Redix.PubSub process happens via Elixir messages (that simulate a Pub/Sub interaction with the pub/sub server).

{:ok, pubsub} = Redix.PubSub.start_link()

Redix.PubSub.subscribe(pubsub, "my_channel", self())
#=> {:ok, ref}

Confirmation of subscriptions is delivered as an Elixir message:

receive do
  {:redix_pubsub, ^pubsub, ^ref, :subscribed, %{channel: "my_channel"}} -> :ok
end

If someone publishes a message on a channel we're subscribed to:

receive do
  {:redix_pubsub, ^pubsub, ^ref, :message, %{channel: "my_channel", payload: "hello"}} ->
    IO.puts("Received a message!")
end

More information on usage of this library can be found in the documentation.

License

ISC 2016, Andrea Leopardi (see LICENSE.txt)