View Source Gnat.Server behaviour (gnat v1.9.0)
A behavior for acting as a server for nats messages.
You can use this behavior in your own module and then use the Gnat.ConsumerSupervisor
to listen for and respond to nats messages.
Example
defmodule MyApp.RpcServer do
use Gnat.Server
def request(%{body: _body}) do
{:reply, "hi"}
end
# defining an error handler is optional, the default one will just call Logger.error for you
def error(%{gnat: gnat, reply_to: reply_to}, _error) do
Gnat.pub(gnat, reply_to, "Something went wrong and I can't handle your request")
end
end
Link to this section Summary
Callbacks
Called when an error occured during the request/1
Called when a message is received from the broker
Link to this section Callbacks
Specs
error(message :: Gnat.message(), error :: term()) :: :ok | {:reply, iodata()}
Called when an error occured during the request/1
If your request/1
function returned {:error, term}
, then the term
you returned will be passed as the second argument.
If an exception was raised during your request/1
function, then the exception will be passed as the second argument.
If your request/1
function returned something other than the supported return types, then its return value will be passed as the second argument.
Specs
request(message :: Gnat.message()) :: :ok | {:reply, iodata()} | {:error, term()}
Called when a message is received from the broker