EmqttFailover

pipeline status coverage report

Wrapper around emqtt which provides support for failing over between brokers.

Examples

To publish messages to a topic:

iex> {:ok, pid} = EmqttFailover.Connection.start_link(
...    configs: [
...      "mqtts://test.mosquitto.org:8886", 
...      "mqtt://test.mosquitto.org:1883"])
iex> EmqttFailover.Connection.publish(pid, 
...    %EmqttFailover.Message{topic: "topic", payload: "payload", qos: 1})
:ok

To subscribe to topics and react to the messages, implement the EmqttFailover.ConnectionHandler behaviour:

defmodule Handler do
  @behaviour EmqttFailover.ConnectionHandler
  
  def init(_opts) do
    {:ok, []}
  end
  
  def handle_connected(state) do
    {:ok, ["topic/a", "topic/b"], state}
  end
  
  def handle_message(%EmqttFailover.Message{} = _message, state) do
    # react to the message
    {:ok, state}
  end
end

iex> {:ok, pid} = EmqttFailover.Connection.start_link(
...    configs: ["mqtts://test.mosquitto.org:8886"],
...    handler: Handler)

Installation

The package can be installed by adding emqtt_failover to your list of dependencies in mix.exs:

def deps do
  [
    {:emqtt_failover, "~> 0.3"}
  ]
end

Documentation

View the full API documentation on Hexdocs or GitLab Pages.

Local development

> docker compose start
> mix test  # or mix coveralls.html
> mix format
> mix credo --strict
> mix dialyzer --format dialyzer
> mix docs

License

Code from emqtt is (c) EMQ Technologies Co, Ltd. under the Apache License, Version 2.0.

Other code is released under the MIT license.