Giter VIP home page Giter VIP logo

eredis_sub's Introduction

Elixir CI Hex.pm Version

EredisSub

Wraps eredis and eredis_sub functionallity for Elixir usage.

  • Publishes binary messages to Redis Pub/Sub channels.
  • Subscribes to channels and calls a handler function when a message is received.

Usage

Publish

EredisSub.publish("my_channel", "Hello, world!")

Subscribe

Implement the behaviour to be called when a message is received:

defmodule MyModule do
  @behaviour EredisSub.Handler

  @impl EredisSub.Handler
  def handle_pubsub_message(message, metadata) do
    # Do something...
  end
end

Subscribe to a channel:

metadata_example = %{subscribed_at: DateTime.utc_now()}
EredisSub.subscribe("my_channel", MyModule, metadata_example)

Installation

def deps do
  [
    {:eredis_sub, "~> 0.1.0"}
  ]
end

Add the following to your supervision tree on application.ex:

children = [
  EredisSub
]

Optional configuration can be passed to eredis and eredis_sub, check their docs:

children = [
  {EredisSub, [database: 2, username: "foo", password: "bar"]}
]

Motivations

Why not use Phoenix.PubSub?

Because multiple applications in many programming languages can use Redis Pub/Sub, but they don't serialize their binary messages according to Phoenix schema.

Why not use eredis directly?

That's also a great option. With EredisSub Elixir abstraction we intend to hide Erlang specific knowledge, for example Erlang strings and OTP processes. We changed the API to let clients subscribe a handler function to a Pub/Sub channel, similar to how :telemetry attaches handlers.

We sacrificed flexibility on client process architecture, for a simpler mental model. If you need to handle message passing or a pool of processes, for example to handle heavier loads, use eredis directly.

Why not use Redix?

Because it doesn't support Redis Cluster. Since our project already depends on eredis_cluster (thus also depends on eredis), we wanted to stick with it.

eredis_sub's People

Contributors

alexandremcosta avatar

Watchers

Jerry Luk avatar  avatar sf 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.