emitter

package module
v2.0.9 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: EPL-1.0 Imports: 13 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout   = errors.New("emitter: operation has timed out")
	ErrUnmarshal = errors.New("emitter: unable to unmarshal the response")
)

Various emitter errors

Functions

func WithAutoReconnect

func WithAutoReconnect(a bool) func(*Client)

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

func WithBrokers

func WithBrokers(brokers ...string) func(*Client)

WithBrokers configures broker URIs to connect to. The format should be scheme://host:port Where "scheme" is one of "tcp", "ssl", or "ws", "host" is the ip-address (or hostname) and "port" is the port on which the broker is accepting connections.

func WithClientID

func WithClientID(id string) func(*Client)

WithClientID 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 WithConnectTimeout

func WithConnectTimeout(t time.Duration) func(*Client)

WithConnectTimeout 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 WithKeepAlive

func WithKeepAlive(k time.Duration) func(*Client)

WithKeepAlive 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 WithMaxReconnectInterval

func WithMaxReconnectInterval(t time.Duration) func(*Client)

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

func WithPassword

func WithPassword(password string) func(*Client)

WithPassword 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 WithPingTimeout

func WithPingTimeout(k time.Duration) func(*Client)

WithPingTimeout 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 WithTLSConfig

func WithTLSConfig(t *tls.Config) func(*Client)

WithTLSConfig 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 WithUsername

func WithUsername(username string) func(*Client)

WithUsername 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.

Types

type Client

type Client struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Client represents an emitter client which holds the connection.

func Connect

func Connect(host string, handler MessageHandler, options ...func(*Client)) (*Client, error)

Connect is a convenience function which sets a broker and connects to it.

func NewClient

func NewClient(options ...func(*Client)) *Client

NewClient will create an MQTT v3.1.1 client with all of the options specified in the provided ClientOptions. The client must have the Connect method called on it before it may be used. This is to make sure resources (such as a net connection) are created before the application is actually ready.

func (*Client) AllowKey added in v2.0.9

func (c *Client) AllowKey(secretKey, targetKey string) (bool, error)

AllowKey sends a request to allow a previously blocked key.

func (*Client) BlockKey added in v2.0.9

func (c *Client) BlockKey(secretKey, targetKey string) (bool, error)

BlockKey sends a request to block a key.

func (*Client) Connect

func (c *Client) Connect() error

Connect initiates a connection to the broker.

func (c *Client) CreateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error)

CreateLink sends a request to create a default link.

func (*Client) Disconnect

func (c *Client) Disconnect(waitTime time.Duration)

Disconnect will end the connection with the server, but not before waiting the specified number of milliseconds to wait for existing work to be completed.

func (*Client) GenerateKey

func (c *Client) GenerateKey(key, channel, permissions string, ttl int) (string, error)

GenerateKey sends a key generation request to the broker

func (*Client) ID

func (c *Client) ID() string

ID retrieves information about the client.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns a bool signifying whether the client is connected or not.

func (*Client) OnConnect

func (c *Client) OnConnect(handler ConnectHandler)

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

func (*Client) OnDisconnect

func (c *Client) OnDisconnect(handler DisconnectHandler)

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

func (*Client) OnError

func (c *Client) OnError(handler ErrorHandler)

OnError will set the function callback to be executed if an emitter-specific error occurs.

func (*Client) OnMessage

func (c *Client) OnMessage(handler MessageHandler)

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

func (*Client) OnPresence

func (c *Client) OnPresence(handler PresenceHandler)

OnPresence sets the function that will be called when a presence event is received.

func (*Client) Presence

func (c *Client) Presence(key, channel string, status, changes bool) error

Presence sends a presence request to the broker.

func (*Client) Publish

func (c *Client) Publish(key string, channel string, payload interface{}, options ...Option) error

Publish will publish a message with the specified QoS and content to the specified topic. Returns a token to track delivery of the message to the broker

func (c *Client) PublishWithLink(name string, payload interface{}, options ...Option) error

PublishWithLink publishes a message with a specified link name instead of a channel key.

func (*Client) PublishWithRetain

func (c *Client) PublishWithRetain(key string, channel string, payload interface{}, options ...Option) error

PublishWithRetain publishes a message with a retain flag set to true

func (*Client) PublishWithTTL

func (c *Client) PublishWithTTL(key string, channel string, payload interface{}, ttl int) error

PublishWithTTL publishes a message with a specified Time-To-Live option

func (*Client) Subscribe

func (c *Client) Subscribe(key string, channel string, optionalHandler MessageHandler, options ...Option) error

Subscribe starts a new subscription. Provide a MessageHandler to be executed when a message is published on the topic provided.

func (*Client) SubscribeWithGroup

func (c *Client) SubscribeWithGroup(key, channel, shareGroup string, optionalHandler MessageHandler, options ...Option) error

SubscribeWithGroup creates a shared subscription to a share group.

func (*Client) SubscribeWithHistory

func (c *Client) SubscribeWithHistory(key string, channel string, last int, optionalHandler MessageHandler) error

SubscribeWithHistory performs a subscribe with an option to retrieve the specified number of messages that were already published in the channel.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(key string, channel string) error

Unsubscribe will end the subscription from each of the topics provided. Messages published to those topics from other clients will no longer be received.

type ConnectHandler

type ConnectHandler func(*Client)

ConnectHandler is a callback that is called when the client state changes from unconnected/disconnected to connected. Both at initial connection and on reconnection

type DisconnectHandler

type DisconnectHandler func(*Client, error)

DisconnectHandler is a callback type which can be set to be executed upon an unintended disconnection from the MQTT broker. Disconnects caused by calling Disconnect or ForceDisconnect will not cause an OnConnectionLost callback to execute.

type Error

type Error struct {
	Request uint16 `json:"req,omitempty"`
	Status  int    `json:"status"`
	Message string `json:"message"`
}

Error represents an event code which provides a more details.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

func (*Error) RequestID

func (e *Error) RequestID() uint16

RequestID returns the request ID for the response.

type ErrorHandler

type ErrorHandler func(*Client, Error)

ErrorHandler is a callback type which can be set to be executed upon the arrival of an emitter error.

type Link struct {
	Request uint16 `json:"req,omitempty"`
	Name    string `json:"name,omitempty"`    // The name of the shortcut, max 2 characters.
	Channel string `json:"channel,omitempty"` // The channel which was registered.
}

Link represents a response for the link creation.

func (*Link) RequestID

func (r *Link) RequestID() uint16

RequestID returns the request ID for the response.

type Message

type Message interface {
	Topic() string
	Payload() []byte
}

Message defines the externals that a message implementation must support these are received messages that are passed to the callbacks, not internal messages

type MessageHandler

type MessageHandler func(*Client, Message)

MessageHandler is a callback type which can be set to be executed upon the arrival of messages published to topics to which the client is subscribed.

type Option

type Option interface {
	String() string
}

Option represents a key/value pair that can be supplied to the publish/subscribe or unsubscribe methods and provide ways to configure the operation.

func WithAtLeastOnce

func WithAtLeastOnce() Option

WithAtLeastOnce instructs to publish at least once (MQTT QoS 1).

func WithAtMostOnce

func WithAtMostOnce() Option

WithAtMostOnce instructs to publish at most once (MQTT QoS 0).

func WithFrom added in v2.0.5

func WithFrom(from time.Time) Option

WithFrom request messages from a point in time.

func WithLast

func WithLast(messages int) Option

WithLast constructs an option which can be used during subscribe requests to retrieve a message history.

func WithRetain

func WithRetain() Option

WithRetain constructs an option which sets the message 'retain' flag to true.

func WithTTL

func WithTTL(seconds int) Option

WithTTL constructs an option which can be used during publish requests to set a Time-To-Live.

func WithUntil added in v2.0.5

func WithUntil(until time.Time) Option

WithUntil request messages until a point in time.

func WithoutEcho

func WithoutEcho() Option

WithoutEcho constructs an option which disables self-receiving messages if subscribed to a channel.

type PresenceEvent

type PresenceEvent struct {
	Who []PresenceInfo
	// contains filtered or unexported fields
}

PresenceEvent represents a response from emitter broker which contains presence state or a join/leave notification.

func (*PresenceEvent) RequestID

func (r *PresenceEvent) RequestID() uint16

RequestID returns the request ID for the response.

type PresenceHandler

type PresenceHandler func(*Client, PresenceEvent)

PresenceHandler is a callback type which can be set to be executed upon the arrival of presence events.

type PresenceInfo

type PresenceInfo struct {
	ID       string `json:"id"`
	Username string `json:"username"`
}

PresenceInfo represents a response from emitter broker which contains presence information.

type Response

type Response interface {
	RequestID() uint16
}

Response represents a generic response sent by the broker.

Jump to

Keyboard shortcuts

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