View Source EredisSub (eredis_sub v0.1.0)
Publishes binary messages to Redis Pub/Sub channels. Subscribes to channels and calls a handler function when a message is received.
Usage
Publish
EredisSub.Server.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.Server.subscribe("my_channel", MyModule, metadata_example)
Add the following to your supervision tree:
children = [
EredisSub
]
Optional configuration can be passed to eredis
and eredis_sub
, check their docs.
Summary
Functions
Publish a message to a channel. If successfull, returns the number of subscribers that received the message. It should never error, unless there is a connection problem.
Subscribe to a channel.
Unsubscribe all handlers from a channel.
Functions
Publish a message to a channel. If successfull, returns the number of subscribers that received the message. It should never error, unless there is a connection problem.
Examples
iex> EredisSub.publish("my_channel", "Hello, world!")
{:ok, 0}
Subscribe to a channel.
Examples
iex> channel = "my channel"
...> metadata = %{}
...>
...> defmodule FooBar do
...> def handle_pubsub_message(_message, _metadata) do
...> # Do something...
...> end
...> end
...>
...> EredisSub.subscribe(channel, FooBar, metadata)
:ok
Unsubscribe all handlers from a channel.
Examples
iex> EredisSub.unsubscribe_all("my_channel")
:ok