Documentation
¶
Overview ¶
Package mqtt is a minimal MQTT 3.1.1 client: enough to feed motion and status into mosquitto / Home Assistant / Frigate, and to hear another tool's motion (Subscribe), without pulling in a full client library. A Client publishes QoS 0 or QoS 1, reconnects on the next Publish after any error, and uses keep-alive 0 so an idle connection is never dropped by the broker; Subscribe keeps its own connection alive with pings.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Subscribe ¶ added in v0.2.3
func Subscribe(ctx context.Context, o SubscribeOptions, topics []string, fn func(topic string, payload []byte))
Subscribe listens on topics (QoS 0 and 1) and calls fn with each message until ctx ends, reconnecting and subscribing again whenever the connection drops. fn runs on the reading goroutine, so it should not block for long.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client publishes to a single broker. It is safe for concurrent use; each Publish is serialized. A dropped connection is re-established on the next Publish, so a broker restart heals itself.
func New ¶
New returns a client for broker addr ("host:port"). clientID must be unique on the broker; user and pass may be empty for an anonymous broker.
func (*Client) Close ¶
Close drops the connection. The client may be reused afterwards.
With a presence topic set it says offline first and then disconnects properly, which tells the broker to discard the last will. Without the DISCONNECT the broker would publish the will as well, at a moment of its choosing -- possibly after a replacement client has already said online.
func (*Client) Publish ¶
Publish sends payload to topic. qos is 0 or 1; a QoS 1 publish waits for the broker's PUBACK. retain asks the broker to keep the message as the topic's last known value.
func (*Client) SetPresence ¶ added in v0.2.5
SetPresence makes every connection this client opens say whether it is up: the broker is asked to publish offline to topic, retained, if the connection is lost without a goodbye (MQTT's last will), and online is published there, retained, as each connection comes up. That is what Home Assistant reads as availability. Call it before the first Publish.
func (*Client) Shutdown ¶ added in v0.2.5
func (c *Client) Shutdown()
Shutdown is Close for good: a Publish afterwards fails instead of opening a new connection. A client with a presence topic that is being replaced needs this, because a late Publish from some goroutine that has not noticed yet would reconnect it and say online again, after the replacement's own online and under the same client id.
type SubscribeOptions ¶ added in v0.2.3
type SubscribeOptions struct {
Addr string
ClientID string
User string
Pass string
// Retry is the pause before reconnecting after the connection ends,
// 5 s when zero.
Retry time.Duration
// OnError, when set, hears why each connection ended.
OnError func(error)
// SkipRetained drops the retained messages a broker replays on
// subscribing. A command topic needs it: a retained "PRESS" would
// sound a siren every time the connection came back.
SkipRetained bool
}
SubscribeOptions is the broker and login for Subscribe.