-module(mqtt). -behaviour(gen_server). -export([start_link/0, init/1, handle_cast/2, handle_info/2, handle_call/3]). -export([publish/2, publish/3]). start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). init(_) -> {ok, PID} = emqtt:start_link(), {ok, Props} = emqtt:connect(PID), {ok, {PID, Props}}. handle_info(_, State) -> {noreply, State}. handle_cast(_, State) -> {noreply, State}. publish(Topic, Payload) -> gen_server:call(?MODULE, {publish, Topic, Payload, []}). publish(Topic, Payload, Options) -> gen_server:call(?MODULE, {publish, Topic, Payload, Options}). handle_call({publish, Topic, Payload, Options}, _, {PID, Props}) -> ok = emqtt:publish(PID, Topic, Payload, Options), {reply, ok, {PID, Props}}.