mqtt

package module
v0.0.0-...-5e47c47 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2020 License: MIT Imports: 19 Imported by: 0

README

MQTT Broker

Why this ?

MQTT broker plugin in go-plugins cuts some features of MQTT client, like that it doesn't support to set MQTT client id, mqtt client username and password, qos etc.

go-plugins's MQTT broker uses github.com/eclipse/paho.mqtt.golang to build MQTT client, but it dose not have all options of paho.mqtt.golang, it drops some.

The plugin github.com/dspo/broker/mqtt add some options.

Usage

The usage is same as github.com/micro/go-plugins/broker/mqtt, just add some additional broker options and APIs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBroker

func NewBroker(opts ...broker.Option) broker.Broker

func NewCodec

func NewCodec(c io.ReadWriteCloser) codec.Codec

func Registry

func Registry(r registry.Registry) broker.Option

func Secure

func Secure(b bool) broker.Option

func SetAutoReconnect

func SetAutoReconnect(a bool) broker.Option

SetAutoReconnect sets whether the automatic reconnection logic should be used when the connection is lost, even if disabled the ConnectionLostHandler is still called

func SetBinaryWill

func SetBinaryWill(topic string, payload []byte, qos byte, retained bool) broker.Option

SetBinaryWill accepts a []byte will message to be set. When the client connects, it will give this will message to the broker, which will then publish the provided payload (the will) to any clients that are subscribed to the provided topic.

func SetCleanSession

func SetCleanSession(clean bool) broker.Option

SetCleanSession will set the "clean session" flag in the connect message when this client connects to an MQTT broker. By setting this flag, you are indicating that no messages saved by the broker for this client should be delivered. Any messages that were going to be sent by this client before diconnecting previously but didn't will not be sent upon connecting to the broker.

func SetClientID

func SetClientID(id string) broker.Option

SetClientID will set the client id to be used by this client when connecting to the MQTT broker. According to the MQTT v3.1 specification, a client id mus be no longer than 23 characters.

func SetCodec

func SetCodec(c codec.Marshaler) broker.Option

func SetConnectTimeout

func SetConnectTimeout(t time.Duration) broker.Option

SetConnectTimeout limits how long the client will wait when trying to open a connection to an MQTT server before timeing out and erroring the attempt. A duration of 0 never times out. Default 30 seconds. Currently only operational on TCP/TLS connections.

func SetConnectionLostHandler

func SetConnectionLostHandler(onLost func(MQTT.Client, error)) broker.Option

SetConnectionLostHandler will set the OnConnectionLost callback to be executed in the case where the client unexpectedly loses connection with the MQTT broker.

func SetCredentialsProvider

func SetCredentialsProvider(p func() (username string, password string)) broker.Option

SetCredentialsProvider will set a method to be called by this client when connecting to the MQTT broker that provide the current username and password. Note: without the use of SSL/TLS, this information will be sent in plaintext accross the wire.

func SetDefaultPublishHandler

func SetDefaultPublishHandler(defaultHandler func(MQTT.Client, MQTT.Message)) broker.Option

SetDefaultPublishHandler sets the MessageHandler that will be called when a message is received that does not match any known subscriptions.

func SetHTTPHeaders

func SetHTTPHeaders(h http.Header) broker.Option

SetHTTPHeaders sets the additional HTTP headers that will be sent in the WebSocket opening handshake.

func SetKeepAlive

func SetKeepAlive(k time.Duration) broker.Option

SetKeepAlive will set the amount of time (in seconds) that the client should wait before sending a PING request to the broker. This will allow the client to know that a connection has not been lost with the server.

func SetMaxReconnectInterval

func SetMaxReconnectInterval(t time.Duration) broker.Option

SetMaxReconnectInterval sets the maximum time that will be waited between reconnection attempts when connection is lost

func SetMessageChannelDepth

func SetMessageChannelDepth(s uint) broker.Option

SetMessageChannelDepth sets the size of the internal queue that holds messages while the client is temporairily offline, allowing the application to publish when the client is reconnecting. This setting is only valid if AutoReconnect is set to true, it is otherwise ignored.

func SetOnConnectHandler

func SetOnConnectHandler(onConn func(MQTT.Client)) broker.Option

SetOnConnectHandler sets the function to be called when the client is connected. Both at initial connection time and upon automatic reconnect.

func SetOrderMatters

func SetOrderMatters(order bool) broker.Option

SetOrderMatters will set the message routing to guarantee order within each QoS level. By default, this value is true. If set to false, this flag indicates that messages can be delivered asynchronously from the client to the application and possibly arrive out of order.

func SetPassword

func SetPassword(p string) broker.Option

SetPassword will set the password to be used by this client when connecting to the MQTT broker. Note: without the use of SSL/TLS, this information will be sent in plaintext accross the wire.

func SetPingTimeout

func SetPingTimeout(k time.Duration) broker.Option

SetPingTimeout will set the amount of time (in seconds) that the client will wait after sending a PING request to the broker, before deciding that the connection has been lost. Default is 10 seconds.

func SetProtocolVersion

func SetProtocolVersion(pv uint) broker.Option

SetProtocolVersion sets the MQTT version to be used to connect to the broker. Legitimate values are currently 3 - MQTT 3.1 or 4 - MQTT 3.1.1

func SetPubQos

func SetPubQos(qos byte)

func SetResumeSubs

func SetResumeSubs(resume bool) broker.Option

SetResumeSubs will enable resuming of stored (un)subscribe messages when connecting but not reconnecting if CleanSession is false. Otherwise these messages are discarded.

func SetRetained

func SetRetained(b bool)

func SetStore

func SetStore(s MQTT.Store) broker.Option

SetStore will set the implementation of the Store interface used to provide message persistence in cases where QoS levels QoS_ONE or QoS_TWO are used. If no store is provided, then the client will use MemoryStore by default.

func SetSubQos

func SetSubQos(qos byte)

func SetTLSConfig

func SetTLSConfig(t *tls.Config) broker.Option

SetTLSConfig will set an SSL/TLS configuration to be used when connecting to an MQTT broker. Please read the official Go documentation for more information.

func SetUsername

func SetUsername(u string) broker.Option

SetUsername will set the username to be used by this client when connecting to the MQTT broker. Note: without the use of SSL/TLS, this information will be sent in plaintext accross the wire.

func SetWill

func SetWill(topic string, payload string, qos byte, retained bool) broker.Option

SetWill accepts a string will message to be set. When the client connects, it will give this will message to the broker, which will then publish the provided payload (the will) to any clients that are subscribed to the provided topic.

func SetWriteTimeout

func SetWriteTimeout(t time.Duration) broker.Option

SetWriteTimeout puts a limit on how long a mqtt publish should block until it unblocks with a timeout error. A duration of 0 never times out. Default 30 seconds

func UnsetWill

func UnsetWill() broker.Option

UnsetWill will cause any set will message to be disregarded.

Types

type Codec

type Codec struct {
	Conn    io.ReadWriteCloser
	Encoder *json.Encoder
	Decoder *json.Decoder
}

a json codec, an implementation of github.com/micro/go-micro/codec.Codec it tries to do marshal/unmarshal by github.com/golang/protobuf/jsonpb as possible.

func (*Codec) Close

func (c *Codec) Close() error

func (*Codec) ReadBody

func (c *Codec) ReadBody(b interface{}) error

func (*Codec) ReadHeader

func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error

func (*Codec) String

func (c *Codec) String() string

func (*Codec) Write

func (c *Codec) Write(m *codec.Message, b interface{}) error

type NoopMarshaler

type NoopMarshaler struct{}

a marshaller not care about message header

func NewNoopMarshaler

func NewNoopMarshaler() NoopMarshaler

func (NoopMarshaler) Marshal

func (n NoopMarshaler) Marshal(v interface{}) ([]byte, error)

func (NoopMarshaler) String

func (n NoopMarshaler) String() string

func (NoopMarshaler) Unmarshal

func (n NoopMarshaler) Unmarshal(d []byte, v interface{}) error

type Option

type Option func(options *Options)

type Options

type Options struct {
	*MQTT.ClientOptions
	*broker.Options
}

type Subscribers

type Subscribers interface {
	Subscribe(topic string, eventHandler broker.Handler, opts ...broker.SubscribeOption) error
	Unsubscribe(topic string) error
	GetSubscriber(topic string) (broker.Subscriber, error)
}

func GetSubscribers

func GetSubscribers() Subscribers

API for managing sub/unsub

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL