Hivent
The official Elixir client for Hivent.
Installation
-
Add
hiventto your list of dependencies inmix.exs:
def deps do
[{:hivent, "~> 0.1.0"}]
end-
Ensure
hiventis started before your application:
def application do
[applications: [:hivent]]
endConfiguration
In your <env>.exs file:
config :hivent,
max_reconnect_tries: 3,
reconnect_backoff_time: 10,
client_id: "hivent_test",
server: %{
host: "<instance_id>.hivent.io",
port: 443,
secure: true,
api_key: "<Secret API Key>"
}Here's what every option does:
max_reconnect_tries: The maximum number of reconnections the client will attempt after losing a connection. Once all the tries are up, the server is allowed to crash.reconnect_backoff_time: The exponential amount of time to wait before reconnecting.client_id: Only useful if producing events. Every event's metadata carries aproducerkey with this config as its value.server:host: The host for your Hivent instanceport: The port for your Hivent instancesecure: Use a secure (wss) or insecure (ws) socket connectionapi_key: Your API key for the Hivent instance.
Emitting events
Hivent.emit("some:event", %{foo: "bar"}, %{version: 1})
The Hivent.Emitter process is automatically put into a supervision tree, so it will start automatically upon crashing.
Consuming events
defmodule MyApp.Consumer do
@topic "some:event"
@name "test_consumer"
@partition_count 2
use Hivent.Consumer
def process(%Hivent.Event{} = event) do
if do_something(event) do
:ok
else
# Puts the event in the dead letter queue
{:error, "failed to process"}
end
end
end
defmodule MyApp.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
worker(MyApp.Consumer, [])
]
opts = [
strategy: :one_for_one,
name: MyApp.Supervisor,
max_restarts: 50
]
Supervisor.start_link(children, opts)
end
end
Your consumers implementing the Hivent.Consumer behaviour must be supervised manually, if you wish to do so. If you fail
to supervise them, they will not be restarted automatically after crashing