Streams commands observed by Redis' MONITOR command.
Redis.Monitor owns a dedicated connection because a connection in monitor
mode cannot be used for ordinary commands. Each record is parsed into a
Redis.Monitor.Entry and sent to subscribers:
{:ok, monitor} = Redis.Monitor.start_link(port: 6379)
:ok = Redis.Monitor.subscribe(monitor)
receive do
{:redis_monitor, %Redis.Monitor.Entry{} = entry} ->
IO.inspect(entry)
endA Redis URI can be passed instead of connection options:
Redis.Monitor.start_link("redis://:secret@localhost:6379/2")Subscriber filters
Filters are applied independently for each subscriber:
Redis.Monitor.subscribe(monitor,
commands: ["SET", "DEL"],
database: 0,
client: ~r/^127.0.0.1:/
)Supported filters are:
:commands- a command or list of commands, matched case-insensitively:database- a database number or list of database numbers:client- an exact client identifier or a regular expression
Connection options
The connection options mirror Redis.Connection where they apply:
:host, :port, :password, :username, :database, :ssl,
:ssl_opts, :socket, :client_name, :credential_provider, :timeout,
:sync_connect, :backoff_initial, and :backoff_max.
MONITOR exposes live application traffic and has a measurable performance
cost. It should be protected and used deliberately in production.
Summary
Functions
Returns a child specification for a monitor process.
Starts a dedicated Redis MONITOR connection.
Stops the monitor connection.
Subscribes a process to parsed monitor entries.
Subscribes subscriber with optional per-subscriber filters.
Returns the currently subscribed process identifiers.
Unsubscribes a process from monitor entries.
Types
@type filter() :: {:commands, String.t() | [String.t()]} | {:database, non_neg_integer() | [non_neg_integer()]} | {:client, String.t() | Regex.t()}
Functions
@spec child_spec(keyword() | String.t()) :: Supervisor.child_spec()
Returns a child specification for a monitor process.
@spec start_link(keyword() | String.t()) :: GenServer.on_start()
Starts a dedicated Redis MONITOR connection.
@spec stop(GenServer.server()) :: :ok
Stops the monitor connection.
@spec subscribe(GenServer.server(), pid() | [filter()]) :: :ok | {:error, term()}
Subscribes a process to parsed monitor entries.
The second argument may be a subscriber pid or a filter keyword list.
@spec subscribe(GenServer.server(), pid(), [filter()]) :: :ok | {:error, term()}
Subscribes subscriber with optional per-subscriber filters.
@spec subscribers(GenServer.server()) :: [pid()]
Returns the currently subscribed process identifiers.
@spec unsubscribe(GenServer.server(), pid()) :: :ok
Unsubscribes a process from monitor entries.