net
ModuleThe network adapter: MQTT over TCP, TLS and WebSocket listeners on tokio.
Contents
use utopia_mqtt::Tokio;pub struct TokioAn 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.
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;use utopia_mqtt::Transport;pub enum TransportA 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 themqttsubprotocol, 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.
pub const DEFAULT_MAX_PACKET_SIZE: usize = 64000The largest packet a listener accepts when none is given, in bytes.
pub const MAX_CONNECTIONS: usize = 100_000The most connections served at once.