Rust

net

Module

The network adapter: MQTT over TCP, TLS and WebSocket listeners on tokio.

Contents

use utopia_mqtt::Tokio;
Rust
pub struct Tokio

An MQTT adapter on the network, on tokio: one listener per Transport, all feeding the same hooks.

start binds every listener, calls the start hook and the worker start hook for each worker, then serves until shutdown. Each connection gets the next id from 1; the receive hook gets one whole MQTT packet at a time. send queues bytes for a connection and returns at once (a WebSocket connection gets a binary message); close writes what is queued, then closes the connection. The timer (a TimingWheel by default) closes a connection when it ticks.

Rust
use utopia_mqtt::{Tokio, Transport};

let adapter = Tokio::new(
    vec![Transport::tcp("0.0.0.0", 1883), Transport::websocket("0.0.0.0", 8083)],
    1,
    None,
)?;
// let server = utopia_mqtt::Server::new(adapter, handler);
// server.start().await;
Compiles in CI; needs a running service to execute.
use utopia_mqtt::Transport;
Rust
pub enum Transport

A listener the Tokio adapter serves MQTT on.

  • Tcp: MQTT on a TCP port (1883 by convention).
  • Tls: MQTT over TLS (8883), with a PEM certificate chain and private key.
  • WebSocket: MQTT in WebSocket binary messages (8083), as browsers speak it (MQTT.js, Paho): the handshake echoes the mqtt subprotocol, and messages are reassembled into whole packets (a message may hold several packets, or part of one).

A packet larger than the listener's maximum closes the connection; on WebSocket, so does a partial packet that can no longer fit.

Rust
pub const DEFAULT_MAX_PACKET_SIZE: usize = 64000

The largest packet a listener accepts when none is given, in bytes.

Rust
pub const MAX_CONNECTIONS: usize = 100_000

The most connections served at once.