# Hivent

The official Elixir client for [Hivent](

* [Read the docs](

## Installation

  1. Add `hivent` to your list of dependencies in `mix.exs`:

  def deps do
    [{:hivent, "~> 0.1.0"}]

  2. Ensure `hivent` is started before your application:

  def application do
    [applications: [:hivent]]

## Configuration

In your `<env>.exs` file:

config :hivent,
  max_reconnect_tries: 3,
  reconnect_backoff_time: 10,
  client_id: "hivent_test",
  server: %{
    host: "<instance_id>",
    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 a `producer` key with this config as its value.
* `server`:
  * `host`: The host for your Hivent instance
  * `port`: The port for your Hivent instance
  * `secure`: Use a secure (`wss`) or insecure (`ws`) socket connection
  * `api_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
        # Puts the event in the dead letter queue
        {:error, "failed to process"}

  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)

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