# `MqttX.Transport`
[🔗](https://github.com/cignosystems/mqttx/blob/v0.9.0/lib/mqttx/transport/transport.ex#L1)

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

# `handler`

```elixir
@type handler() :: module()
```

# `handler_opts`

```elixir
@type handler_opts() :: term()
```

# `socket`

```elixir
@type socket() :: term()
```

# `transport_opts`

```elixir
@type transport_opts() :: keyword()
```

# `close`

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

Close the connection.

# `getopts`
*optional* 

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

Get socket options.

# `peername`

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

Get the remote address of the connection.

# `send`

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

Send data over the connection.

# `setopts`
*optional* 

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

Set socket options.

# `start_link`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
