Documentation ¶
Index ¶
- Constants
- type MqttClient
- func (mqttClient *MqttClient) ConnectWithAccessToken(hostPort string, userName string, accessToken string) error
- func (mqttClient *MqttClient) ConnectWithClientCert(hostPort string, clientCert *tls.Certificate) error
- func (mqttClient *MqttClient) Disconnect()
- func (mqttClient *MqttClient) Publish(topic string, message []byte) error
- func (mqttClient *MqttClient) PublishObject(topic string, object interface{}) error
- func (mqttClient *MqttClient) SetPrettyPrint(enable bool)
- func (mqttClient *MqttClient) Subscribe(topic string, handler func(address string, message []byte))
- func (mqttClient *MqttClient) Unsubscribe(topic string)
- type TopicSubscription
Constants ¶
const DefaultKeepAliveSec = 10
DefaultKeepAliveSec time a keep alive ping is sent. This is the max wait time to discover a broken connection
const DefaultTimeoutSec = 3
DefaultTimeoutSec constant with connection, reconnection and disconnection timeouts
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MqttClient ¶
type MqttClient struct {
// contains filtered or unexported fields
}
MqttClient client wrapper around pahoClient This addresses problems with reconnect and auto resubscribe while using clean session
func NewMqttClient ¶
func NewMqttClient(appID string, caCert *x509.Certificate, timeoutSec int) *MqttClient
NewMqttClient creates a new MQTT messenger instance.
See ConnectWithAccessToken or ConnectWithClientCert for the two methods of authentication. To avoid hanging, keep the timeout low, if 0 is provided the default of 3 seconds is used
appID application ID to include in the MQTT client ID caCert with the x509 CA certificate for validating the server connection timeoutSec to attempt connecting before it is considered failed. Use 0 for default (3)
func (*MqttClient) ConnectWithAccessToken ¶
func (mqttClient *MqttClient) ConnectWithAccessToken(hostPort string, userName string, accessToken string) error
ConnectWithAccessToken connects to the MQTT broker using an access token for authentication If a previous connection exists then it is disconnected first. If no connection is possible this keeps retrying until the timeout is expired. With each retry a backoff period is increased until 120 seconds.
hostPort with address and port for MQTT password authentication userName to identify as. Must match access token. password access token to identify with
func (*MqttClient) ConnectWithClientCert ¶
func (mqttClient *MqttClient) ConnectWithClientCert(hostPort string, clientCert *tls.Certificate) error
ConnectWithClientCert connects to the MQTT broker using client certificate authentication
hostPort with address and port for certificate authentication clientCert client TLS certificate to authenticate the client with the broker. This certificate must be signed by the CA of the broker, so that the broker can authenticate the client.
func (*MqttClient) Disconnect ¶
func (mqttClient *MqttClient) Disconnect()
Disconnect the connection to the MQTT broker and unsubscribe from all addresss and set device state to disconnected
func (*MqttClient) Publish ¶
func (mqttClient *MqttClient) Publish(topic string, message []byte) error
Publish a message to a topic address
func (*MqttClient) PublishObject ¶
func (mqttClient *MqttClient) PublishObject(topic string, object interface{}) error
PublishObject marshals an object into json and publishes it to the given topic If jsonIndent is provided then the message is formatted nicely for humans
func (*MqttClient) SetPrettyPrint ¶
func (mqttClient *MqttClient) SetPrettyPrint(enable bool)
SetPrettyPrint enables/disables pretty-print in marshalling json
func (*MqttClient) Subscribe ¶
func (mqttClient *MqttClient) Subscribe( topic string, handler func(address string, message []byte))
Subscribe to a address If a subscription already exists, it is replaced. topic: address to subscribe to. This supports mqtt wildcards such as + and # handler: callback handler.
func (*MqttClient) Unsubscribe ¶
func (mqttClient *MqttClient) Unsubscribe(topic string)
Unsubscribe a topic
type TopicSubscription ¶
type TopicSubscription struct {
// contains filtered or unexported fields
}
TopicSubscription holds subscriptions to restore after disconnect