Coder Social home page Coder Social logo

Comments (2)

asabil avatar asabil commented on August 20, 2024

Hi @mynameisrufus, this library is useful if you want low level MQTT support, and thus requires a bit more setup in order to create a server using it. Among other things, you need to setup your listening socket yourself.

Here is some (untested) example code using ranch as the socket listener.

defmodule MyMqttServer.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    opts = [
      strategy: :one_for_one,
      name: MyMqttServer.Supervisor
    ]

    children = [
      ranch_listeners(ip: {127, 0, 0, 1},  port: 8883)
    ]

    Supervisor.start_link(children, opts)
  end

  defp ranch_listeners(config) do
    :ranch.child_spec(:my_mqtt_server, 10, :ranch_tcp, options, MyMqttServer.Connection, [])
  end
end

defmodule MyMqttServer.Connection do
  use MQTT.Server

  def start_link(ref, ranch_socket, ranch_transport, options) do
    :proc_lib.start_link(__MODULE__, :init, [ref, ranch_socket, ranch_transport, options])
  end

  def stop(pid) do
    MQTT.Server.stop(pid, :normal, 500)
  end

  def init(ref, ranch_socket, ranch_transport, _options) do
    :ok = :proc_lib.init_ack({:ok, self()})
    :ok = :ranch.accept_ack(ref)
    transport = MQTT.Transport.new(ranch_transport.name(), ranch_socket)
    MQTT.Server.enter_loop(__MODULE__, [], transport)
  end

   def init(_args, %{protocol: "MY-PROTOCOL/1.0"}) do
    {:ok, :my_state}
  end
  def init(_args, _) do
    {:stop, :unacceptable_protocol}
  end
  
  def handle_publish(topic, message, _opts, s) do
    IO.inspect("publication requested")
    {:ok, state}
  end

  def handle_subscribe(topic, _qos, s) do
    IO.inspect("subscription requested")
    {:ok, :failed, s} # Reject all subscriptions
  end

  def handle_unsubscribe(topic, s) do
    IO.inspect("unsubscription requested")
    {:ok, s}
  end

  def handle_info(_info, s) do
    {:ok, s}
  end

  def terminate(_reason, _) do
    :ok
  end
end

Please note that this is a Low level library, it is useful if you want to use the mqtt wire protocol without necessarily implementing the entire protocol. Also, as of now QoS other than 0 are not supported.

Hope this helps.

from erlang-mqtt.

mynameisrufus avatar mynameisrufus commented on August 20, 2024

Thanks @asabil,

I will see if I can write some tests and or documentation for this and make a PR. I love the approach of the low level lib, just needs some more doco, I will see what I can do.

from erlang-mqtt.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.