Skip to content

Commit

Permalink
Move documentation from server to eredis_sub
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremcosta committed Jun 11, 2024
1 parent e9cecd4 commit f21de24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
43 changes: 32 additions & 11 deletions lib/eredis_sub.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
defmodule EredisSub do
@moduledoc """
Documentation for `EredisSub`.
"""
Publishes binary messages to Redis Pub/Sub channels.
Subscribes to channels and calls a handler function when a message is received.
def start_link(config \\ []) do
EredisSub.Server.start_link(config)
end
## Usage
def child_spec(config) do
EredisSub.Server.child_spec(config)
end
### 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(message, metadata) do
# Do something...
end
end
Subscribe to a channel:
metadata_example = %{subscribed_at: DateTime.utc_now()}
EredisSub.Server.subscribe("my_channel", {MyModule, :handle, metadata_example})
"""
alias EredisSub.Server

def start_link(config \\ []), do: Server.start_link(config)
def child_spec(config), do: Server.child_spec(config)

@doc """
Publish a message to a channel.
Expand All @@ -20,7 +41,7 @@ defmodule EredisSub do
:ok
"""
def publish(channel, message) do
EredisSub.Server.publish(channel, message)
Server.publish(channel, message)
end

@doc """
Expand All @@ -41,7 +62,7 @@ defmodule EredisSub do
:ok
"""
def subscribe(channel, handler) do
EredisSub.Server.subscribe(channel, handler)
Server.subscribe(channel, handler)
end

@doc """
Expand All @@ -53,6 +74,6 @@ defmodule EredisSub do
:ok
"""
def unsubscribe_all(channel) do
EredisSub.Server.unsubscribe_all(channel)
Server.unsubscribe_all(channel)
end
end
34 changes: 2 additions & 32 deletions lib/eredis_sub/server.ex
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
defmodule EredisSub.Server do
@moduledoc """
Publishes binary messages to Redis Pub/Sub channels.
Subscribes to channels and calls a handler function when a message is received.
## Usage
### Publish
```elixir
EredisSub.Server.publish("my_channel", "Hello, world!")
```
### Subscribe
Implement the behaviour to be called when a message is received:
```elixir
defmodule MyModule do
@behaviour EredisSub.Handler
@impl EredisSub.Handler
def handle(message, metadata) do
# Do something...
end
end
```
Subscribe to a channel:
```elixir
metadata_example = %{subscribed_at: DateTime.utc_now()}
EredisSub.Server.subscribe("my_channel", {MyModule, :handle, metadata_example})
```
Holds internal state for `EredisSub`.
Do not use this module directly. Instead, use `EredisSub`.
"""

use GenServer
Expand Down

0 comments on commit f21de24

Please sign in to comment.