Behaviour for the handler consumption style of ExNtfy.subscribe/2.
Pass handler: {MyHandler, init_arg} and the subscription invokes your
callbacks in its own process instead of sending messages to an owner:
defmodule MyHandler do
@behaviour ExNtfy.Handler
@impl true
def init(arg), do: {:ok, arg}
@impl true
def handle_message(%ExNtfy.Message{} = message, state) do
IO.puts("#{message.topic}: #{message.message}")
{:ok, state}
end
@impl true
def handle_lifecycle(:connected, state), do: {:ok, state}
def handle_lifecycle(_event, state), do: {:ok, state}
endhandle_lifecycle/2 is optional; lifecycle events are dropped without it.
Callbacks run inside the subscription process — an exception terminates the
subscription (put it under your own supervisor to restart it).
Summary
Types
Lifecycle notifications: connection state changes, terminal {:down, reason}, and message_clear/message_delete events with their parsed
message.
Callbacks
Optional: invoked for lifecycle events (see lifecycle_event/0).
Invoked for every :message event.
Invoked once at subscription start; returns the initial handler state.
Types
@type lifecycle_event() :: :connected | :disconnected | {:down, term()} | {:message_clear | :message_delete, ExNtfy.Message.t()}
Lifecycle notifications: connection state changes, terminal {:down, reason}, and message_clear/message_delete events with their parsed
message.
Callbacks
@callback handle_lifecycle(lifecycle_event(), state :: term()) :: {:ok, state :: term()}
Optional: invoked for lifecycle events (see lifecycle_event/0).
@callback handle_message(ExNtfy.Message.t(), state :: term()) :: {:ok, state :: term()}
Invoked for every :message event.
Invoked once at subscription start; returns the initial handler state.