Hivent

The official Elixir client for Hivent.

Installation

  1. Add hivent to your list of dependencies in mix.exs:
  def deps do
    [{:hivent, "~> 0.1.0"}]
  end
  1. Ensure hivent is started before your application:
  def application do
    [applications: [:hivent]]
  end

Configuration

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:

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