Giter Site home page Giter Site logo

redix_pubsub's Introduction

DO NOT USE

This is an attempt to modify the original pub_sub library to work with redis streams instead of redis pub sub.

What works now:

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

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

# We wait for the subscription confirmation
receive do
  {:redix_pubsub, ^pubsub, ^ref, :subscribed, %{channel: "my_channel"}} -> :ok
end

Redix.command!(client, ~w(XADD my_channel * sensor-id 1234 temperature 19.8))

receive do
  {:redix_pubsub, ^pubsub, ^ref, :message, %{channel: "my_channel"} = properties} ->
  properties.payload
end
#=> [["1541027511486-0", ["sensor-id", "1234", "temperature", "19.8"]]]

TODO:

  • use redis streams consumer groups
  • work with unsuscribe (does not work at all at the moment)
  • no tests for xread at the moment

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)

redix_pubsub's People

Contributors

whatyouhide avatar tompave avatar hlavacek avatar merqlove avatar halfdan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.