Documentation ¶
Index ¶
- Variables
- type Client
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) DisconnectImmediately()
- func (c *Client) Handle(topic string, handler MessageHandler) Route
- func (c *Client) Listen(topic string) (chan Message, Route)
- func (c *Client) Publish(ctx context.Context, topic string, payload []byte, qos QOS, ...) error
- func (c *Client) PublishJSON(ctx context.Context, topic string, payload interface{}, qos QOS, ...) error
- func (c *Client) PublishString(ctx context.Context, topic string, payload string, qos QOS, ...) error
- func (c *Client) Subscribe(ctx context.Context, topic string, qos QOS) error
- func (c *Client) SubscribeMultiple(ctx context.Context, subscriptions map[string]QOS) error
- func (c *Client) Unsubscribe(ctx context.Context, topic string) error
- type ClientOptions
- type Message
- type MessageHandler
- type PublishOption
- type QOS
- type Route
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMinimumOneServer means that at least one server should be specified in the client options ErrMinimumOneServer = errors.New("mqtt: at least one server needs to be specified") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { Options ClientOptions // The options that were used to create this client // contains filtered or unexported fields }
Client for talking using mqtt
func NewClient ¶
func NewClient(options ClientOptions) (*Client, error)
NewClient creates a new client with the specified options
func (*Client) DisconnectImmediately ¶
func (c *Client) DisconnectImmediately()
DisconnectImmediately will immediately close the connection with the mqtt servers
func (*Client) Handle ¶
func (c *Client) Handle(topic string, handler MessageHandler) Route
Handle adds a handler for a certain topic. This handler gets called if any message arrives that matches the topic. Also returns a route that can be used to unsubsribe. Does not automatically subscribe.
func (*Client) Listen ¶
Listen returns a stream of messages that match the topic. Also returns a route that can be used to unsubsribe. Does not automatically subscribe.
func (*Client) Publish ¶
func (c *Client) Publish(ctx context.Context, topic string, payload []byte, qos QOS, options ...PublishOption) error
Publish a message with a byte array payload
func (*Client) PublishJSON ¶
func (c *Client) PublishJSON(ctx context.Context, topic string, payload interface{}, qos QOS, options ...PublishOption) error
PublishJSON publishes a message with the payload encoded as JSON using encoding/json
func (*Client) PublishString ¶
func (c *Client) PublishString(ctx context.Context, topic string, payload string, qos QOS, options ...PublishOption) error
PublishString publishes a message with a string payload
func (*Client) SubscribeMultiple ¶ added in v0.2.0
SubscribeMultiple subscribes to multiple topics and errors if this fails.
type ClientOptions ¶
type ClientOptions struct { Servers []string // The list of broker hostnames to connect to ClientID string // If left empty a uuid will automatically be generated Username string // If not set then authentication will not be used Password string // Will only be used if the username is set AutoReconnect bool // If the client should automatically try to reconnect when the connection is lost }
ClientOptions is the list of options used to create a client
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
A Message from or to the broker
func (*Message) Acknowledge ¶
func (m *Message) Acknowledge()
Acknowledge explicitly acknowledges to a broker that the message has been recieved
func (*Message) IsDuplicate ¶
IsDuplicate is true if this exact message has been recieved before (due to a AtLeastOnce QOS)
func (*Message) PayloadJSON ¶
PayloadJSON unmarshals the payload into the provided interface using encoding/json and returns an error if anything fails
func (*Message) PayloadString ¶
PayloadString returns the payload as a string
type MessageHandler ¶
type MessageHandler func(Message)
A MessageHandler to handle incoming messages
type PublishOption ¶
type PublishOption int
PublishOption are extra options when publishing a message
const ( // Retain tells the broker to retain a message and send it as the first message to new subscribers. Retain PublishOption = iota )
type QOS ¶
type QOS byte
QOS describes the quality of service of an mqtt publish
const ( // AtMostOnce means the broker will deliver at most once to every subscriber - this means message delivery is not guaranteed AtMostOnce QOS = iota // AtLeastOnce means the broker will deliver a message at least once to every subscriber AtLeastOnce // ExactlyOnce means the broker will deliver a message exactly once to every subscriber ExactlyOnce )