Quick Start
If available in Hex, the package can be installed as:
Add
busto your list of dependencies inmix.exs:def deps do [{:bus, "~> 0.1.4"}] endEnsure
busis started before your application:def application do [applications: [:bus]] endAdd Configuration Details in
config.exfile.config :bus, host: 'localhost', port: 1883, client_id: "1", #needs to be string. keep_alive: 100, #this is in seconds. username: "", password: "", auto_reconnect: true, #if client get disconnected, it will auto reconnect. auto_connect: true, #this will make sure when you start :bus process, it gets connected autometically callback: Bus.Callback #callback module, you need to implement callback inside.Publish
topic = "a" message = "Hello World...!" qos = 1 callback_fun = fn(message) -> IO.puts message end #This function will be called when this perticular message will be published. Bus.Mqtt.publish(topic,message,callback_fun,qos)Subscribe
topics = ["a","b","c"] #list of topics qoses = [1,0,2] #list of qos in same order as topics. callback_fun = fn(message) -> IO.puts message end #This function will be called when this perticular topic will be subscribed. Bus.Mqtt.subscribe(topics,qoses,callback_fun)Callback
defmodule Bus.Callback do #removed, no longer supported. #def on_publish(data) do # IO.inspect data #end def on_connect(data) do IO.inspect data end def on_disconnect(data) do IO.inspect data end def on_error(data) do IO.inspect data end def on_info(data) do IO.inspect data end #removed, no longer supported. #def on_subscribe(data) do # IO.inspect data #end #removed, no longer supported. #def on_unsubscribe(data) do # IO.inspect data #end def on_message_received(topic,message) do IO.inspect topic IO.inspect message end endThis still needs more improvements, may change in upcoming versions.
Note
Detailed Documentation is coming soon.