Hi,
These days I am trying to publish data to the mqtt broker with nerves project.
When I try to use the GenMQTT.pulish I find some question.
Below is my code.
defmodule QT do
use GenMQTT
def start_link do
{:ok, pid} = GenMQTT.start_link(__MODULE__, {}, opt())
pid
end
def pb(pidd, data) do
GenMQTT.publish(pidd, "JP_JACK", data, 1)
end
def store_pb(data) do
pid = start_link()
pb(pid, data)
end
def pipe_pb(data) do
start_link() |> pb(data)
end
defp opt do
[host: "broker.hivemq.com", port: 1883]
end
After run mix deps.get
I run with the iex -S mix
, when I type QT.store.pb("message")
and QT.pipe("message")
both of the result is :ok
which should be successful publish. But I can’t get the message. I use the MQTTFX to subscribe the same topic.
However I run the below it works.
pid = QT.start_link
QT.pb(pid, "message")
The result is same as before which is :ok
, but with these method I can get the “message” at the
MQTTFX.
These make me really confused.
Appreciate for all the advice and help. Also sorry for the poor typesetting.