Mqttc

A fast and robust MQTT v5 client for Elixir.
It aims to provide a reliable MQTT client with a simple user-facing API.

Features


Installation

Add mqttc to your dependencies in mix.exs:

def deps do
  [
    {:mqttc, "~> 0.2"}
  ]
end

Usage:

# Connect
{:ok, pid} = Mqttc.start_link(host: "test.mosquitto.org", port: 1883)
# or Start connection and wait until CONNACK received.  
{:ok, pid} = Mqttc.start_link_sync(host: "test.mosquitto.org", port: 1883)


# Subscribe to topics with handlers
Mqttc.subscribe(pid, [
  {"sensors/temp", fn msg -> IO.inspect(msg, label: "Temperature") end},
  {"sensors/humidity", fn msg -> IO.inspect(msg, label: "Humidity") end}
])

# Publish 
Mqttc.publish(pid, "sensors/temp", "25°C")

# Unsubscribe
Mqttc.unsubscribe(pid, "sensors/humidity")

# Disconnect
Mqttc.disconnect(pid)

Authenticate and connect:

{:ok, pid} =  Mqttc.start_link_sync( host: "test.mosquitto.org", port: 1884, username: "rw", password: "readwrite" )

Usage with ssl:

{:ok, pid} = Mqttc.start_link(host: "broker.hivemq.com", port: 8883, ssl: true, ssl_opts: [  verify: :verify_peer, cacerts: :public_key.cacerts_get()])

Test:

Integration test requires a running mosquitto instance.

mix test # run all tests, 
mix test --exclude integration  # test packet encoding and decoding, excludes tests with mosquitto

License

Mqttc is released under the MIT license. See the license file.