Giter Club home page Giter Club logo

exmqttc's Introduction

Exmqttc Deps Status Build Status Inline docs

Elixir wrapper for the emqttc library.

emqttc must currently be installed manually as it is not available on hex (yet).

Installation

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

def deps do
  [{:exmqttc, "~> 0.5"}, {:emqttc, github: "emqtt/emqttc"}]
end

Usage

Create a callback module:

defmodule MyClient do
  require Logger
  use Exmqttc.Callback

  def init(_params) do
    {:ok, []}
  end

  def handle_connect(state) do
    Logger.debug "Connected"
    {:ok, state}
  end

  def handle_disconnect(state) do
    Logger.debug "Disconnected"
    {:ok, state}
  end

  def handle_publish(topic, payload, state) do
    Logger.debug "Message received on topic #{topic} with payload #{payload}"
    {:ok, state}
  end
end

You can keep your own state and return it just like with :gen_server.

Start the MQTT connection process by calling start_link/3:

{:ok, pid} = Exmqttc.start_link(MyClient, [], [host: '127.0.0.1'], params)

The third argument is a list of emqtt connection options, supporting the following options

  • host: Connection host, charlist, default: 'localhost'
  • port: Connection port, integer, default 1883
  • client_id: Binary ID for client, automatically set to UUID if not specified
  • clean_sess: MQTT cleanSession flag. true disables persistent sessions on the server
  • keepalive: Keepalive timer, integer
  • username: Login username, binary
  • password: Login password, binary
  • will: Last will, keywordlist, sample: [qos: 1, retain: false, topic: "WillTopic", payload: "I died"]
  • connack_timeout: Timeout for connack package, integer, default 60
  • puback_timeout: Timeout for puback package, integer, default 8
  • suback_timeout: Timeout for suback package, integer, default 4
  • ssl: List of ssl options
  • auto_resub: Automatically resubscribe to topics, boolean, default: false
  • reconnect: Automatically reconnect on lost connection, integer (), default false

Params are passed to the init function of your callback module.

You can publish messages to the given PID:

Exmqttc.publish(pid, "test", "foo")

publish/4 also supports passing in QOS and retain options:

Exmqttc.publish(pid, "test", "foo", qos: 2, retain: true)

Further docs can be found at https://hexdocs.pm/exmqttc.

exmqttc's People

Contributors

timbuchwaldt avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.