Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More examples would be good. #1

Open
shymega opened this issue Jun 8, 2017 · 11 comments
Open

More examples would be good. #1

shymega opened this issue Jun 8, 2017 · 11 comments

Comments

@shymega
Copy link

shymega commented Jun 8, 2017

Just what the title says, tbh. Lack of examples does make it hard to understand.

@timbuchwaldt
Copy link
Owner

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.

@timbuchwaldt
Copy link
Owner

@shymega I just added new docs for the newly released version 0.3.1 - this should solve this issue :)

@shymega
Copy link
Author

shymega commented Jun 16, 2017

@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?

@timbuchwaldt timbuchwaldt reopened this Jun 16, 2017
@timbuchwaldt
Copy link
Owner

What does the GenServer do in this case? Can you show a little excerpt from how you use it?

@shymega
Copy link
Author

shymega commented Jun 16, 2017

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?

@timbuchwaldt
Copy link
Owner

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.

@shymega
Copy link
Author

shymega commented Jun 16, 2017

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!

@shymega
Copy link
Author

shymega commented Sep 18, 2017

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?

@ulutomaz
Copy link

ulutomaz commented Oct 8, 2017

+1 GenServer example

@chintan-mishra
Copy link

chintan-mishra commented Nov 1, 2017

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.

@chintan-mishra
Copy link

chintan-mishra commented Nov 1, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants