More examples would be good.
Just what the title says, tbh. Lack of examples does make it hard to understand.
Hi Shymega, you are right, currently I lack time to add some, but will expand on samples/docs as soon as I have some spare time.
@shymega I just added new docs for the newly released version 0.3.1 - this should solve this issue :)
@timbuchwaldt Thanks for that, but just wondering - could we add some for GenServer impls, as well? I'm working primarily with GenServer, and it works pretty much the same as the callback, 'xcept I have to run this command in the init() func:
Process.register(self(), :testclient) for the module to run.
Any ideas?
What does the GenServer do in this case? Can you show a little excerpt from how you use it?
Sure thing. This is the code behind the MQTT module of my project. (Feel free to poke around!)
Without "Process.register/2", the code fails to run, and kills itself. I think. Not an expert in Elixir.
I got the code from your test suite for this project, but I think I'm doing it wrong.
WDYT?
Ah now I get it.. ok, so your are basically using my TestClient - this wasn't quite what I intended users to do. The TestClient is basically just an implementation of the callback interface so I can run unit tests against it.
Process.register/2 is needed as this was just meant to be used for my unit tests - I would encourage you to copy my test client into your project and alter it for your needs.
I see. I used that approach /because/ of the lack of documentation. Obviously there's no excuse for my approach now.
I'll try using the callback approach.. but ideally, I'd prefer to use a GenServer type solution.
Cheers!
OK, so I tried the callback approach, but I still prefer GenServer.
I tried using emqttc directly, but it didn't work the way necessary. It kept timing out and not sending pings to the broker..
Could we add some GenServer examples, for new users?
+1 GenServer example
I am thinking of contributing by building a simple example using Exmqttc showing its use with supervisors and GenServer. But I am stuck right at the first step. So, if you can provide any kind of help it'd help us build a go to example for using Exmqttc.
In the code MQTT.Callback is an implementation of MyClient in README.
defmodule MQTT do
def publish() do
{:ok, pid} = Exmqttc.start_link(MQTT.Callback, [name: :client], host: '127.0.0.1')
Exmqttc.publish(pid, "test", "elixir's payload")
:timer.sleep(500)
Exmqttc.disconnect(pid)
end
end
The above code shows the first version which works like a charm.
iex> MQTT.publish()
<time> [debug] Connected
:ok
Things start to break when I add GenServer to take it a step further.
defmodule MQTT do
use GenServer
##############
# Client API #
##############
def start_link(_) do
GenServer.start_link(__MODULE__, :ok, [])
end
def publish() do
GenServer.call(__MODULE__, {:publish})
end
#############
# Callbacks #
#############
def init(_) do
{:ok , %{}}
end
def handle_call({:publish}, _from, state) do
{:ok, pid} = Exmqttc.start_link(MQTT.Callback, [name: :client], host: '127.0.0.1')
Exmqttc.publish(pid, "test", "elixir's payload")
{:reply, state, state}
end
end
I get following error. Any help regarding where did I go wrong would be appreciated.
iex(1)> MQTT.start_link([])
{:ok, #PID<0.175.0>}
iex(2)> MQTT.publish()
** (exit) exited in: GenServer.call(MQTT, {:publish}, 5000)
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(elixir) lib/gen_server.ex:766: GenServer.call/3
I am using mosquitto_sub and mosquitto_pub to test my messages and VerneMQ as the preferred broker.
Solved the issue. Please check this out and I would love to receive comments about the scope of improvements.
How to work with the example?
iex>{:ok, pid} = MQTT.Worker.start_link([])
iex>MQTT.Worker.sub(pid)
iex>MQTT.Worker.pub(pid)
sub subscribes to the topic test and displays the message.
pub publishes the message elixir's payload to the topic test.
A slight modification will allow you to add the option of selecting the topic, client details, quality of service etc.