MqttX.Transport behaviour (MqttX v0.7.0)
View SourceBehaviour 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.
Start the transport server.
Types
Callbacks
@callback close(socket()) :: :ok
Close the connection.
@callback getopts(socket(), [:inet.socket_getopt()]) :: {:ok, [:inet.socket_setopt()]} | {:error, term()}
Get socket options.
@callback peername(socket()) :: {:ok, {:inet.ip_address(), :inet.port_number()}} | {:error, term()}
Get the remote address of the connection.
Send data over the connection.
@callback setopts(socket(), [:inet.socket_setopt()]) :: :ok | {:error, term()}
Set socket options.
@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.