Herald is a library to exchange async messages between systems through a message broker.
These library support AMQP based message brokers
Add in your mix.exs and run mix deps.get:
def deps do
[
{:herald, "~> 0.1.0"}
]
end- Create you message schema:
defmodule MyApp.Message do
use Herald.Message
payload do
field :age, :integer
field :name, :string, required: true
end
end- Create your processor function
Processor function will work as a callback function when a message is received.
defmodule MyApp.Message do
use Herald.Message
payload do
field :age, :integer
field :name, :string, required: true
end
def my_processor(%MyApp.Message{} = message) do
if do_some_stuff_with(message) do
{:ok, message}
else
{:error, :my_bad_error}
end
end
end- Create your Router
Router will say for library what schema and processor use for each queue.
In example bellow, any message received in queue my_queue will be validated by schema MyApp.Message, and processed by function &MyApp.Message.my_processor/1.
defmodule MyApp.Router do
use Herald.Router
route "my_queue",
schema: MyApp.Message,
processor: &MyApp.Message.my_processor/1
end- Configure this
Finaly, need inform Herald what is the module where router is configured:
config :herald,
router: Develop.RouterPublications can be performed as bellow:
alias Herald.AMQP.Publisher
MyApp.Message.new("queue", %{
"age" => 70,
"name" => "Dennis Ritchie"
})
|> Publisher.perform()Docs for this library can be found at https://hexdocs.pm/herald.