{"action":"created","issue":{"url":"https://api.github.com/repos/gausby/gen_mqtt/issues/12","repository_url":"https://api.github.com/repos/gausby/gen_mqtt","labels_url":"https://api.github.com/repos/gausby/gen_mqtt/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/gausby/gen_mqtt/issues/12/comments","events_url":"https://api.github.com/repos/gausby/gen_mqtt/issues/12/events","html_url":"https://github.com/gausby/gen_mqtt/issues/12","id":184034628,"number":12,"title":"Unable to Subscribe","user":{"login":"neildsouza","id":13505179,"avatar_url":"https://avatars.githubusercontent.com/u/13505179?v=3","gravatar_id":"","url":"https://api.github.com/users/neildsouza","html_url":"https://github.com/neildsouza","followers_url":"https://api.github.com/users/neildsouza/followers","following_url":"https://api.github.com/users/neildsouza/following{/other_user}","gists_url":"https://api.github.com/users/neildsouza/gists{/gist_id}","starred_url":"https://api.github.com/users/neildsouza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neildsouza/subscriptions","organizations_url":"https://api.github.com/users/neildsouza/orgs","repos_url":"https://api.github.com/users/neildsouza/repos","events_url":"https://api.github.com/users/neildsouza/events{/privacy}","received_events_url":"https://api.github.com/users/neildsouza/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-10-19T18:09:13Z","updated_at":"2016-10-19T20:02:41Z","closed_at":null,"body":"Hi,\r\n\r\nHave been trying (unsuccessfully) to Subscribe to a topic using my GenMQTT Callback module.\r\n\r\n```elixir\r\ndefmodule MqttBridge.ConnectionProcess do\r\n    use GenMQTT\r\n\r\n    def start_link do\r\n        GenMQTT.start_link(__MODULE__, :ok, [name: __MODULE__])\r\n    end\r\n\r\n    def on_connect(state) do\r\n        :ok = GenMQTT.subscribe(self, \"Elixir_Test/In\", 0)\r\n        {:ok, state}\r\n    end\r\n    \r\n    def on_connect_error(reason, state) do\r\n        IO.puts reason\r\n        {:ok, state}\r\n    end\r\n    \r\n    def on_subscribe(list, state) do\r\n        IO.puts list\r\n        {:ok, state}\r\n    end\r\n\r\n    def handle_info(msg, state) do\r\n        IO.puts msg\r\n        {:no_reply, state}\r\n    end\r\nend\r\n```\r\nI seem to be getting errors in the on_subscribe callback.\r\n```elixir\r\n23:34:27.312 [error] ** State machine MqttBridge.ConnectionProcess terminating \r\n** Last message in was {:tcp, #Port<0.4977>, <<144, 3, 0, 1, 0>>}\r\n** When State == :connected\r\n**      Data  == {:state, 'localhost', 1883, #Port<0.4977>, 2, :undefined, :undefined,\r\n \"455D38CF1A353ACFCEE8\", true, :undefined, :undefined, 0, \"\", :undefined, 60000,\r\n 10000, 3, MqttBridge.ConnectionProcess, :ok, {:gen_tcp, []}, \"\",\r\n {:dict, 2, 16, 16, 8, 80, 48,\r\n  {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},\r\n  {{[[{:subscribe, 1} |\r\n      {#Reference<0.0.2.226>, {:subscribe, [{\"Elixir_Test/In\", 0}]}}]], [], [],\r\n    [], [], [], [], [[:ping | #Reference<0.0.2.225>]], [], [], [], [], [], [],\r\n    [], []}}}, {#Function<0.128699311/2 in :gen_emqtt.init/1>, :ok}}\r\n** Reason for termination = \r\n** {:badarg,\r\n [{:io, :put_chars, [#PID<0.150.0>, :unicode, [[{\"Elixir_Test/In\", 0}], 10]],\r\n   []},\r\n  {MqttBridge.ConnectionProcess, :on_subscribe, 2,\r\n   [file: 'lib/mqtt_bridge/connection_process.ex', line: 19]},\r\n  {:gen_emqtt, :wrap_res, 4,\r\n   [file: '/home/neil/programs/elixir_progs/mqtt_bridge/deps/vmq_commons/src/gen_emqtt.erl',\r\n    line: 129]},\r\n  {:gen_emqtt, :process_bytes, 3,\r\n   [file: '/home/neil/programs/elixir_progs/mqtt_bridge/deps/vmq_commons/src/gen_emqtt.erl',\r\n    line: 346]}, {:gen_fsm, :handle_msg, 7, [file: 'gen_fsm.erl', line: 451]},\r\n  {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}\r\n```\r\nAny thoughts on whats causing this :("},"comment":{"url":"https://api.github.com/repos/gausby/gen_mqtt/issues/comments/254924290","html_url":"https://github.com/gausby/gen_mqtt/issues/12#issuecomment-254924290","issue_url":"https://api.github.com/repos/gausby/gen_mqtt/issues/12","id":254924290,"user":{"login":"gausby","id":482052,"avatar_url":"https://avatars.githubusercontent.com/u/482052?v=3","gravatar_id":"","url":"https://api.github.com/users/gausby","html_url":"https://github.com/gausby","followers_url":"https://api.github.com/users/gausby/followers","following_url":"https://api.github.com/users/gausby/following{/other_user}","gists_url":"https://api.github.com/users/gausby/gists{/gist_id}","starred_url":"https://api.github.com/users/gausby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gausby/subscriptions","organizations_url":"https://api.github.com/users/gausby/orgs","repos_url":"https://api.github.com/users/gausby/repos","events_url":"https://api.github.com/users/gausby/events{/privacy}","received_events_url":"https://api.github.com/users/gausby/received_events","type":"User","site_admin":false},"created_at":"2016-10-19T20:02:40Z","updated_at":"2016-10-19T20:02:40Z","body":"`handle_info/2` is used when another process in the system sends the process a message; `handle_info/2` does not concern itself with what's coming in on the MQTT connection. You can try this by obtaining the pid or the process that runs the GenMQTT behaviour and send it a message with `send msg, \"hello\"`—in that case you would see `\"hello\"` getting printed.\r\n\r\nI think you are looking for `on_publish/3`, try dropping this into the code and post something to the \"Elixir_Test/In\" topic on the MQTT server:\r\n```elixir\r\ndef on_publish([\"Elixir_Test\", \"In\"], message, state) do\r\n    IO.inspect message\r\n    {:ok, state}\r\nend\r\n```\r\nDoes that work?"}}
{"actor":{"display_login":"gausby"}}