redix_pubsub_fastlane v0.1.2 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 :phoenix_pubsub_redis
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
def fastlane(payload, options) do
IO.inspect(payload)
IO.inspect(options)
end
end
{:ok, _pubsub} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)
Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "my_channel", {My.Fastlane, [:some_id]})
#=> :ok
After a subscription, messages published to a channel are delivered My.Fastlane.fastlane/2
,
as it subscribed to that channel via Redix.PubSub.Fastlane
:
Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "my_channel", "hello")
#=> :ok
#=> "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, ...}}
.
Any existing subscriptions will not be overrided. If you want to update one, then unsubscribe and subscribe it again.
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", {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/2
callbacks which accepts similar format:
def fastlane(%{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 serverpattern
- The pattern to subscribe to, ie:"ba*"
fastlane
- The tuple with fastlane module and it’s arguments, ie:{My.Fastlane, [:some_id]}
Examples
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {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
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "users:123", "hello")
:ok
"hello"
[:some_id]
Unsubscribes fastlane from the PubSub adapter’s by pattern.
server
- The Pid registered name of the serverpattern
- The pattern to unsubscribe from, ie:"ba*"
Examples
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {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.Redisoptions
- 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 serverchannel
- The channel to subscribe to, ie:"users:123"
fastlane
- The tuple with fastlane module and it’s arguments, ie:{My.Fastlane, [:some_id]}
Examples
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
Unsubscribes fastlane from the PubSub adapter by channel.
server
- The Pid registered name of the serverchannel
- The channel to unsubscribe from, ie:"users:123"
Examples
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.unsubscribe(MyApp.PubSub.Redis, "users:123")
:ok
Functions
Specs
psubscribe(atom, String.t, term) :: :ok
Subscribes fastlane to the PubSub adapter by pattern.
server
- The Pid registered name of the serverpattern
- The pattern to subscribe to, ie:"ba*"
fastlane
- The tuple with fastlane module and it’s arguments, ie:{My.Fastlane, [:some_id]}
Examples
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {My.Fastlane, [:some_id]})
:ok
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
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.publish(MyApp.PubSub.Redis, "users:123", "hello")
:ok
"hello"
[:some_id]
Specs
punsubscribe(atom, String.t) :: :ok
Unsubscribes fastlane from the PubSub adapter’s by pattern.
server
- The Pid registered name of the serverpattern
- The pattern to unsubscribe from, ie:"ba*"
Examples
iex> Redix.PubSub.Fastlane.psubscribe(MyApp.PubSub.Redis, "ba*", {My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.punsubscribe(MyApp.PubSub.Redis, "ba*")
:ok
Specs
start_link(atom, Keyword.t) :: :ok | {:error, term}
Creates new PubSub Supervisor process
name
- Server name, like MyApp.PubSub.Redisoptions
- The Server options, default:[]
Examples
iex> {:ok, _} = Redix.PubSub.Fastlane.start_link(MyApp.PubSub.Redis)
:ok
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
Specs
subscribe(atom, binary, term) :: :ok
Subscribes fastlane to the PubSub adapter by channel.
server
- The Pid registered name of the serverchannel
- The channel to subscribe to, ie:"users:123"
fastlane
- The tuple with fastlane module and it’s arguments, ie:{My.Fastlane, [:some_id]}
Examples
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
Specs
unsubscribe(atom, binary) :: :ok
Unsubscribes fastlane from the PubSub adapter by channel.
server
- The Pid registered name of the serverchannel
- The channel to unsubscribe from, ie:"users:123"
Examples
iex> Redix.PubSub.Fastlane.subscribe(MyApp.PubSub.Redis, "users:123", {My.Fastlane, [:some_id]})
:ok
iex> Redix.PubSub.Fastlane.unsubscribe(MyApp.PubSub.Redis, "users:123")
:ok