Documentation
¶
Overview ¶
Package mqttconn is a minimal MQTT v3.1.1 client: Dial, Publish (QoS 0/1), Subscribe, and Close. No third-party dependency — the CONNECT handshake and packet read/write are implemented directly on top of net.Conn, mirroring how wsconn implements just enough of RFC 6455 rather than depending on a full-featured library.
Deliberately narrow: no QoS 2, no persistent sessions, no offline message queuing, no TLS helpers beyond what net.Dial itself provides. It exists to serve inovelli/mqtt's Zwave JS UI gateway path, whose needs are exactly "publish a config write" and "subscribe to value-change updates" — not to be a general-purpose MQTT library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is one connected MQTT v3.1.1 session. A single goroutine may call Publish/Subscribe concurrently with each other and with Close; only one background goroutine ever reads from the connection.
func Dial ¶
Dial connects to an MQTT v3.1.1 broker at addr ("host:port"), completes the CONNECT/CONNACK handshake, and starts the background read and keep-alive ping loops.
func (*Client) Close ¶
Close sends DISCONNECT and closes the underlying connection, unblocking the read loop and every in-flight Publish/Subscribe call. Safe to call more than once, or concurrently with the read loop noticing the connection died on its own — whichever happens first wins the close error; cleanup always happens.
func (*Client) Publish ¶
Publish sends payload to topic at qos (0 or 1 — QoS 2 isn't implemented, this package has no consumer that needs its exactly-once handshake). QoS 1 blocks until the broker's PUBACK arrives or ctx is done; QoS 0 returns as soon as the packet is written.
func (*Client) Subscribe ¶
func (c *Client) Subscribe(ctx context.Context, filter string, handler func(topic string, payload []byte)) error
Subscribe registers handler for every message published to a topic matching filter ("+" for one level, "#" for the rest, per MQTT's topic matching rules), and blocks until the broker's SUBACK arrives or ctx is done. handler is called from its own goroutine per message, so it may block without stalling the connection's read loop.
type Options ¶
type Options struct {
ClientID string
Username string
Password string
KeepAlive time.Duration // default 60s
}
Options configures Dial. The zero value is a reasonable default: a random client ID, no auth, 60s keep-alive. Every session is a clean session (no persistent subscriptions or queued messages across reconnects) — this package has no use for offline delivery guarantees.