A Microservices Toolkit. Provides a simple, composable framework for exchanging messages in a distributed system. Currently supports AMQP message brokers, but it has only been tested with RabbitMQ so far.
Microbrew uses configuration specified in your config.exs file to connect to a
RabbitMQ instance. The default configuration is:
config :microbrew, :rabbitmq,
username: "guest",
password: "guest",
host: "localhost",
port: 5672Agents are at the core of Microbrew.
Not to be mistaken with an Elixir.Agent, a Microbrew.Agent is an entity that
can be used to receive and emit signals in a distributed system.
You can create an agent using the new method:
Microbrew.Agent.new(
exchange: "an_exchange",
queue: "a_queue",
queue_error: "an_error_queue",
options: [
exchange: [
type: :fanout,
durable: true
]
]
)
# => %Microbrew.Agent{exchange: "an_exchange", queue: "a_queue", queue_error: "an_error_queue"}Using new automatically configures your Agent with Consumer and Producer
capabilities. While producing content has no continuous overhead, a constant
connection is required to consume content, so you may wish not to use .new and
instead create the struct yourself.
agent = %Microbrew.Agent{
exchange: "an_exchange",
queue: "a_queue",
queue_error: "an_error_queue",
}If you already have an agent created without Consumer capabilities, you can
add them by calling consume:
agent = %Microbrew.Agent{
exchange: "an_exchange",
queue: "a_queue",
queue_error: "an_error_queue",
}
agent |> consume |> ...A signal models an event in the system. It is mainly used to compose into the
on and emit methods.
agent |> signal "temperature::new"
# => %Signal{agent: agent, event: "temperature::new"}Sets up a consumer for a given Signal. Currently the only consumer supported
is :data. It is triggered whenever any kind of data payload that matches your
Signal comes in through the wire. Payloads are decoded from JSON into Maps.
agent
|> signal "temperature::new"
|> on :data, fn (payload, meta) ->
# Do something with "payload" and "meta"
endLets you access Signal data as a Stream. Each value in the stream is a tuple
containing the payload and meta information.
agent
|> signal("sensor::received")
|> stream
# #Function<25.29647706/2 in Stream.resource/3>Publishes a payload under a given Signal. Payloads are published in
JSON format.
signal "temperature::new"
|> emit "hello world"
# => {:ok}Stops an Agent. This effectively shuts down any Consumer behaviour for the
given Agent. You can still publish, though. To restart Consumer behaviour use
.consume.
agent |> stop
# => :ok