MqttX.Transport behaviour (MqttX v0.7.0)

View Source

Behaviour for MQTT transport adapters.

Transport adapters handle the underlying network connections (TCP, TLS, WebSocket, etc.) and forward data to the MQTT protocol handler.

Implementing a Transport

defmodule MyTransport do
  @behaviour MqttX.Transport

  @impl true
  def start_link(handler, handler_opts, transport_opts) do
    # Start the transport server
  end

  @impl true
  def send(socket, data) do
    # Send data over the connection
  end

  @impl true
  def close(socket) do
    # Close the connection
  end

  @impl true
  def peername(socket) do
    # Get the peer address
  end
end

Summary

Callbacks

Close the connection.

Get socket options.

Get the remote address of the connection.

Send data over the connection.

Set socket options.

Types

handler()

@type handler() :: module()

handler_opts()

@type handler_opts() :: term()

socket()

@type socket() :: term()

transport_opts()

@type transport_opts() :: keyword()

Callbacks

close(socket)

@callback close(socket()) :: :ok

Close the connection.

getopts(socket, list)

(optional)
@callback getopts(socket(), [:inet.socket_getopt()]) ::
  {:ok, [:inet.socket_setopt()]} | {:error, term()}

Get socket options.

peername(socket)

@callback peername(socket()) ::
  {:ok, {:inet.ip_address(), :inet.port_number()}} | {:error, term()}

Get the remote address of the connection.

send(socket, iodata)

@callback send(socket(), iodata()) :: :ok | {:error, term()}

Send data over the connection.

setopts(socket, list)

(optional)
@callback setopts(socket(), [:inet.socket_setopt()]) :: :ok | {:error, term()}

Set socket options.

start_link(handler, handler_opts, transport_opts)

@callback start_link(handler(), handler_opts(), transport_opts()) ::
  {:ok, pid()} | {:error, term()}

Start the transport server.

The handler module will receive callbacks for connection events.