redix_pubsub_fastlane v0.1.9 Redix.PubSub.Fastlane

Fastlane pattern based on Redix.PubSub interface.

To use Redix.PubSub.Fastlane, simply add it to your Mix.config:

config :redix_pubsub_fastlane, MyApp.PubSub.Redis,
  fastlane: My.Fastlane,
  host: "192.168.1.100"

For full list of options: Redix.PubSub.Fastlane.Supervisor

You will also need to add :redix_pubsub_fastlane to your deps:

defp deps do
  [{:redix_pubsub_fastlane, "~> 0.1"}]
end

And also add :redix_pubsub_fastlane to your list of applications:

def application do
  [mod: {MyApp, []},
   applications: [..., :redix_pubsub_fastlane]]
end

Usage

Simply add it to your Supervisor stack:

supervisor(Redix.PubSub.Fastlane, [MyApp.PubSub.Redis, [host: "localhost",
                                                        port: 6397,
                                                        pool_size: 5]]),

Or run it by hands:

{:ok, _} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)

Subscription process:

defmodule My.Fastlane do
  use GenServer

  def start_link(opts \\ []), do: GenServer.start_link(__MODULE__, opts, [])

  def init(opts), do: {:ok, opts}

  def fastlane(pid, payload, options) do
    IO.inspect(pid)
    IO.inspect(payload)
    IO.inspect(options)
  end
end
{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
{:ok, _pubsub} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)
Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "my_channel", {pid, My.Fastlane, [:some_id]})
Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "my_channel", {pid, My.Fastlane, [:some_id2]})
Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "my_channel", {pid, My.Fastlane, [:some_id3]})
#=> :ok

After a subscription, messages published to a channel are delivered My.Fastlane.fastlane/3, as it subscribed to that channel via Redix.PubSub.Fastlane:

Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "my_channel", "hello")
#=> :ok
#=> #<PID:444>
#=> "hello"
#=> [:some_id]

If you haven’t provided any fastlane, then you must provide a PID of the receiver process, as a fallback. The wrapper will not touch or store any part of incoming payload, just compare channel with cached one to find suitable fastlane.

About

Works as a simple wrapper over Redix.PubSub.

Main goal is providing a fastlane path for publisher messages like: {:redix_pubsub, _, :message, %{channel: channel, ...}}.

Imagine: You have a task, that has few subtasks each with its own UUID & must await for published event, but also must know main task ID within every event.

Ie:

Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "channel1", {pid, My.Fastlane, ["some_id"]})

If you provide it, the fastlane handler is notified of a cached message instead of the normal subscriber. Fastlane handlers must implement fastlane/3 callbacks which accepts similar format:

def fastlane(pid, %{channel: channel, payload: payload}, [:some_id])

And returns a fastlaned format for the handler.

Summary

Functions

Subscribes fastlane to the PubSub adapter by pattern.

  • server - The Pid registered name of the server
  • pattern - The pattern to subscribe to, ie: "ba*"
  • fastlane - The tuple with fastlane module and it’s arguments, ie: {pid, My.Fastlane, [:some_id]}

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {pid, My.Fastlane, [:some_id]})
:ok

Publish message on given channel. Helper method, mostly for testing.

  • server - The Pid or registered server name, for example: MyApp.PubSub
  • channel - The channel to publish to, ie: "users:123"
  • message - The payload of the publish

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "users:123", "hello")
:ok
#<PID:444>
"hello"
[:some_id]

Unsubscribes fastlane from the PubSub adapter’s by pattern.

  • server - The Pid registered name of the server
  • pattern - The pattern to unsubscribe from, ie: "ba*"

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.punsubscribe(MyApp.PubSub.Redis, "ba*")
:ok

Creates new PubSub Supervisor process

  • name - Server name, like MyApp.PubSub.Redis
  • options - The Server options, default: []

Examples

iex> {:ok, _} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)
:ok

Stops the given PubSub Supervisor process. This function is asynchronous (fire and forget): it returns :ok as soon as it’s called and performs the closing of the connection after that.

Examples

iex> Redix.PubSub.Fastlane.stop(MyApp.PubSub.Redis)
:ok

Subscribes fastlane to the PubSub adapter by channel.

  • server - The Pid registered name of the server
  • channel - The channel to subscribe to, ie: "users:123"
  • fastlane - The tuple with fastlane module and it’s arguments, ie: {pid, My.Fastlane, [:some_id]}

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok

Unsubscribes fastlane from the PubSub adapter by channel.

  • server - The Pid registered name of the server
  • channel - The channel to unsubscribe from, ie: "users:123"

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.unsubscribe(MyApp.PubSub.Redis, "users:123")
:ok

Functions

psubscribe(server, pattern, fastlane)

Specs

psubscribe(atom, String.t, term) :: :ok

Subscribes fastlane to the PubSub adapter by pattern.

  • server - The Pid registered name of the server
  • pattern - The pattern to subscribe to, ie: "ba*"
  • fastlane - The tuple with fastlane module and it’s arguments, ie: {pid, My.Fastlane, [:some_id]}

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {pid, My.Fastlane, [:some_id]})
:ok
publish(server, channel, message)

Specs

publish(atom, binary, term | binary) ::
  :ok |
  {:error, term}

Publish message on given channel. Helper method, mostly for testing.

  • server - The Pid or registered server name, for example: MyApp.PubSub
  • channel - The channel to publish to, ie: "users:123"
  • message - The payload of the publish

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "users:123", "hello")
:ok
#<PID:444>
"hello"
[:some_id]
punsubscribe(server, pattern)

Specs

punsubscribe(atom, String.t) :: :ok

Unsubscribes fastlane from the PubSub adapter’s by pattern.

  • server - The Pid registered name of the server
  • pattern - The pattern to unsubscribe from, ie: "ba*"

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.punsubscribe(MyApp.PubSub.Redis, "ba*")
:ok
start_link(name, options \\ [])

Specs

start_link(atom, Keyword.t) :: :ok | {:error, term}

Creates new PubSub Supervisor process

  • name - Server name, like MyApp.PubSub.Redis
  • options - The Server options, default: []

Examples

iex> {:ok, _} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)
:ok
stop(server)

Specs

stop(atom) :: :ok | :error

Stops the given PubSub Supervisor process. This function is asynchronous (fire and forget): it returns :ok as soon as it’s called and performs the closing of the connection after that.

Examples

iex> Redix.PubSub.Fastlane.stop(MyApp.PubSub.Redis)
:ok
subscribe(server, channel, fastlane)

Specs

subscribe(atom, binary, term) :: :ok

Subscribes fastlane to the PubSub adapter by channel.

  • server - The Pid registered name of the server
  • channel - The channel to subscribe to, ie: "users:123"
  • fastlane - The tuple with fastlane module and it’s arguments, ie: {pid, My.Fastlane, [:some_id]}

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok
unsubscribe(server, channel)

Specs

unsubscribe(atom, binary) :: :ok

Unsubscribes fastlane from the PubSub adapter by channel.

  • server - The Pid registered name of the server
  • channel - The channel to unsubscribe from, ie: "users:123"

Examples

{:ok, pid} = My.Fastlane.start_link
#=> {:ok, #<PID:444>}
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {pid, My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.unsubscribe(MyApp.PubSub.Redis, "users:123")
:ok