Connecting to AWS IOT mqtt

I’ve tried multiple mqtt clients and almost no one has ssl client support except exmqtt that wraps emqttc, using that I’ve tried to connect to aws iot using:

{:ok, pid} = Exmqttc.start_link(MqttClient, [], [
  host: 'xxxxxx.iot.eu-west-1.amazonaws.com',
  port: 8883,
  clean_sess: true,
  ssl: [
    certfile: Application.app_dir(:dem, "priv/mqtt-certificate.pem.crt"),
    keyfile: Application.app_dir(:dem, "priv/mqtt-private.pem.crt")
  ],
  auto_resub: true,
  reconnect: true
], %{})
Exmqttc.subscribe(pid, "#")

however none of the connect/disconnect callbacks are called. My callbacks module is:

defmodule MqttClient do
  require Logger
  use Exmqttc.Callback

  def init(_params) do
    {:ok, []}
  end

  def handle_connect(state) do
    Logger.debug "Connected"
    {:ok, state}
  end

  def handle_disconnect(state) do
    Logger.debug "Disconnected"
    {:ok, state}
  end

  def handle_publish(topic, payload, state) do
    Logger.debug "Message received on topic #{topic} with payload #{payload}"
    {:ok, state}
  end
end

does someone have any idea? Is my elixir config correct? (specially the ssl part which I’m not sure is the right elixir counterpart of the erlang code)

2 Likes

I haven’t tried connecting to a amazon hosted MQTT server, but I do have a client called Tortoise, and it does support SSL. Currently it lacks documentation, and I am about to write some; have you tried it? What’s your experience? Where should I focus in the documentation?

I was thinking I should make a “guides” section in the documentation where topics such as connecting to AWS hosted MQTT could be described.

1 Like

Thanks for the reply, I went through tortoise but without any doc and since I was in a hurry I’ve just used gen_mqtt (which is also yours :slight_smile:) with mosquitto which bridges the client to AWS IOT.

Btw regarding documentation a non-ssl/ssl and an handler example (just saw that in the tests) would be more than enough!

1 Like

Awesome. Perhaps I should write a macro that will make implementing a Tortoise.Handler easier with default implementations for most of the callbacks. Currently I am making an attempt to improve the documentation for Tortoise over at this pull request:

Don’t be shy if you have any feedback.

I red the documentation but I didn’t manage to do the SSL connection with Tortoise. Could you please explain about the options? cacertfile, key, cert. Is it a path? Is it a string of what file contains? what is it ? I couldn’t find documentation for this.

@gausby could you tell me if the Tortoise client supports MQTT v5.0? I know https://github.com/brianbinbin/exmqtt is an Elixir MQTT v5.0 Client compatible with MQTT v3.0.

Not yet; but hopefully I can get to work on Tortoise at some point in the near future. Sorry I have been away for a long while. Feel free to check out other options. The MQTT 5 version of Tortoise will have a different interface than the one that worked for MQTT 3.1.1, so if you have something working already a slight rewrite of your MQTT parts will be required anyways.