{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package mqttspec\n\n## Constants\n\n```go\nconst (\n\t// BeforeHandle fires after model resolution, before operation dispatch\n\tBeforeHandle = websocketspec.BeforeHandle\n\n\t// CRUD operation hooks\n\tBeforeRead   = websocketspec.BeforeRead\n\tAfterRead    = websocketspec.AfterRead\n\tBeforeCreate = websocketspec.BeforeCreate\n\tAfterCreate  = websocketspec.AfterCreate\n\tBeforeUpdate = websocketspec.BeforeUpdate\n\tAfterUpdate  = websocketspec.AfterUpdate\n\tBeforeDelete = websocketspec.BeforeDelete\n\tAfterDelete  = websocketspec.AfterDelete\n\tBeforeScan   = websocketspec.BeforeScan\n\n\t// Subscription hooks\n\tBeforeSubscribe   = websocketspec.BeforeSubscribe\n\tAfterSubscribe    = websocketspec.AfterSubscribe\n\tBeforeUnsubscribe = websocketspec.BeforeUnsubscribe\n\tAfterUnsubscribe  = websocketspec.AfterUnsubscribe\n\n\t// Connection hooks\n\tBeforeConnect    = websocketspec.BeforeConnect\n\tAfterConnect     = websocketspec.AfterConnect\n\tBeforeDisconnect = websocketspec.BeforeDisconnect\n\tAfterDisconnect  = websocketspec.AfterDisconnect\n\n\t// BeforeOp fires immediately before every SQL operation (read, create, update, delete)\n\tBeforeOp = websocketspec.BeforeOp\n\n\t// OnTxBegin fires once, first, inside every transaction the handler opens for a\n\t// read/create/update/delete message (including the second short transaction for\n\t// post-commit work). hookCtx.Tx is the transaction.\n\tOnTxBegin = websocketspec.OnTxBegin\n)\n```\nHook type constants - all lifecycle hooks\n\n```go\nconst (\n\tMessageTypeRequest      = websocketspec.MessageTypeRequest\n\tMessageTypeResponse     = websocketspec.MessageTypeResponse\n\tMessageTypeNotification = websocketspec.MessageTypeNotification\n\tMessageTypeSubscription = websocketspec.MessageTypeSubscription\n\tMessageTypeError        = websocketspec.MessageTypeError\n\tMessageTypePing         = websocketspec.MessageTypePing\n\tMessageTypePong         = websocketspec.MessageTypePong\n)\n```\nMessage type constants\n\n```go\nconst (\n\tOperationRead        = websocketspec.OperationRead\n\tOperationCreate      = websocketspec.OperationCreate\n\tOperationUpdate      = websocketspec.OperationUpdate\n\tOperationDelete      = websocketspec.OperationDelete\n\tOperationSubscribe   = websocketspec.OperationSubscribe\n\tOperationUnsubscribe = websocketspec.OperationUnsubscribe\n\tOperationMeta        = websocketspec.OperationMeta\n)\n```\nOperation type constants\n\n## Variables\n\n```go\nvar (\n\t// NewResponseMessage creates a new response message\n\tNewResponseMessage = websocketspec.NewResponseMessage\n\n\t// NewErrorResponse creates an error response\n\tNewErrorResponse = websocketspec.NewErrorResponse\n\n\t// NewNotificationMessage creates a notification message\n\tNewNotificationMessage = websocketspec.NewNotificationMessage\n\n\t// ParseMessage parses a JSON message into a Message struct\n\tParseMessage = websocketspec.ParseMessage\n)\n```\nHelper functions from websocketspec\n\n## Functions\n\n```go\nfunc RegisterSecurityHooks(handler *Handler, securityList *security.SecurityList)\n```\nRegisterSecurityHooks registers all security-related hooks with the MQTT handler\n\n## Types\n\n```go\ntype AuthConfig struct {\n\t// ValidateCredentials is called to validate username/password for embedded broker\n\t// Return true if credentials are valid, false otherwise\n\tValidateCredentials func(username, password string) bool\n}\n```\nAuthConfig for MQTT-level authentication\n\n```go\ntype BrokerConfig struct {\n\t// Host to bind to (default: \"localhost\")\n\tHost string\n\n\t// Port to listen on (default: 1883)\n\tPort int\n\n\t// EnableWebSocket enables WebSocket support\n\tEnableWebSocket bool\n\n\t// WSPort is the WebSocket port (default: 8883)\n\tWSPort int\n\n\t// MaxConnections limits concurrent client connections\n\tMaxConnections int\n\n\t// KeepAlive is the client keepalive interval\n\tKeepAlive time.Duration\n\n\t// EnableAuth enables username/password authentication\n\tEnableAuth bool\n}\n```\nBrokerConfig configures the embedded Mochi MQTT broker\n\n```go\ntype BrokerInterface interface {\n\t// Start initializes the broker/client connection\n\tStart(ctx context.Context) error\n\n\t// Stop gracefully shuts down the broker/client\n\tStop(ctx context.Context) error\n\n\t// Publish sends a message to a topic\n\tPublish(topic string, qos byte, payload []byte) error\n\n\t// Subscribe subscribes to a topic pattern with callback\n\tSubscribe(topicFilter string, qos byte, callback MessageCallback) error\n\n\t// Unsubscribe removes subscription\n\tUnsubscribe(topicFilter string) error\n\n\t// IsConnected returns connection status\n\tIsConnected() bool\n\n\t// GetClientManager returns the client manager\n\tGetClientManager() *ClientManager\n\n\t// SetHandler sets the handler reference (needed for hooks)\n\tSetHandler(handler *Handler)\n}\n```\nBrokerInterface abstracts MQTT broker operations\n\n```go\ntype BrokerMode string\n```\nBrokerMode specifies how to connect to MQTT\n\n```go\nconst (\n\t// BrokerModeEmbedded runs Mochi MQTT broker in-process\n\tBrokerModeEmbedded BrokerMode = \"embedded\"\n\t// BrokerModeExternal connects to external MQTT broker as client\n\tBrokerModeExternal BrokerMode = \"external\"\n)\n```\n\n```go\ntype Client struct {\n\t// ID is the MQTT client ID (unique per connection)\n\tID string\n\n\t// Username from MQTT CONNECT packet\n\tUsername string\n\n\t// ConnectedAt is when the client connected\n\tConnectedAt time.Time\n\t// contains filtered or unexported fields\n}\n```\nClient represents an MQTT client connection\n\n```go\nfunc NewClient(id, username string, handler *Handler) *Client\n```\nNewClient creates a new MQTT client\n\n```go\nfunc (c *Client) AddSubscription(sub *Subscription)\n```\nAddSubscription adds a subscription to this client\n\n```go\nfunc (c *Client) Close()\n```\nClose cleans up the client\n\n```go\nfunc (c *Client) GetMetadata(key string) (interface{}, bool)\n```\nGetMetadata retrieves metadata for this client\n\n```go\nfunc (c *Client) GetSubscription(subID string) (*Subscription, bool)\n```\nGetSubscription retrieves a subscription by ID\n\n```go\nfunc (c *Client) RemoveSubscription(subID string)\n```\nRemoveSubscription removes a subscription from this client\n\n```go\nfunc (c *Client) SetMetadata(key string, value interface{})\n```\nSetMetadata sets metadata for this client\n\n```go\ntype ClientManager struct {\n\t// contains filtered or unexported fields\n}\n```\nClientManager manages all MQTT client connections\n\n```go\nfunc NewClientManager(ctx context.Context) *ClientManager\n```\nNewClientManager creates a new client manager\n\n```go\nfunc (cm *ClientManager) Count() int\n```\nCount returns the number of active clients\n\n```go\nfunc (cm *ClientManager) GetClient(clientID string) (*Client, bool)\n```\nGetClient retrieves a client by ID\n\n```go\nfunc (cm *ClientManager) Register(clientID, username string, handler *Handler) *Client\n```\nRegister registers a new MQTT client\n\n```go\nfunc (cm *ClientManager) Shutdown()\n```\nShutdown gracefully shuts down the client manager\n\n```go\nfunc (cm *ClientManager) Unregister(clientID string)\n```\nUnregister removes a client\n\n```go\ntype Config struct {\n\t// BrokerMode determines whether to use embedded or external broker\n\tBrokerMode BrokerMode\n\n\t// Broker configuration for embedded mode\n\tBroker BrokerConfig\n\n\t// ExternalBroker configuration for external client mode\n\tExternalBroker ExternalBrokerConfig\n\n\t// Topics configuration\n\tTopics TopicConfig\n\n\t// QoS configuration for different message types\n\tQoS QoSConfig\n\n\t// Auth configuration\n\tAuth AuthConfig\n\n\t// Timeouts for various operations\n\tTimeouts TimeoutConfig\n}\n```\nConfig holds all mqttspec configuration\n\n```go\nfunc DefaultConfig() *Config\n```\nDefaultConfig returns a configuration with sensible defaults\n\n```go\ntype EmbeddedBroker struct {\n\t// contains filtered or unexported fields\n}\n```\nEmbeddedBroker wraps Mochi MQTT server\n\n```go\nfunc NewEmbeddedBroker(config BrokerConfig, clientManager *ClientManager) *EmbeddedBroker\n```\nNewEmbeddedBroker creates a new embedded broker\n\n```go\nfunc (eb *EmbeddedBroker) GetClientManager() *ClientManager\n```\nGetClientManager returns the client manager\n\n```go\nfunc (eb *EmbeddedBroker) IsConnected() bool\n```\nIsConnected returns whether the broker is running\n\n```go\nfunc (eb *EmbeddedBroker) Publish(topic string, qos byte, payload []byte) error\n```\nPublish publishes a message to a topic\n\n```go\nfunc (eb *EmbeddedBroker) SetHandler(handler *Handler)\n```\nSetHandler sets the handler reference\n\n```go\nfunc (eb *EmbeddedBroker) Start(ctx context.Context) error\n```\nStart starts the embedded MQTT broker\n\n```go\nfunc (eb *EmbeddedBroker) Stop(ctx context.Context) error\n```\nStop stops the embedded broker\n\n```go\nfunc (eb *EmbeddedBroker) Subscribe(topicFilter string, qos byte, callback MessageCallback) error\n```\nSubscribe subscribes to a topic\n\n```go\nfunc (eb *EmbeddedBroker) Unsubscribe(topicFilter string) error\n```\nUnsubscribe unsubscribes from a topic\n\n```go\ntype ErrorInfo = websocketspec.ErrorInfo\n```\nErrorInfo contains error details\n\n```go\ntype ExternalBrokerClient struct {\n\t// contains filtered or unexported fields\n}\n```\nExternalBrokerClient wraps Paho MQTT client\n\n```go\nfunc NewExternalBrokerClient(config ExternalBrokerConfig, clientManager *ClientManager) *ExternalBrokerClient\n```\nNewExternalBrokerClient creates a new external broker client\n\n```go\nfunc (ebc *ExternalBrokerClient) GetClientManager() *ClientManager\n```\nGetClientManager returns the client manager\n\n```go\nfunc (ebc *ExternalBrokerClient) IsConnected() bool\n```\nIsConnected returns connection status\n\n```go\nfunc (ebc *ExternalBrokerClient) Publish(topic string, qos byte, payload []byte) error\n```\nPublish publishes a message to a topic\n\n```go\nfunc (ebc *ExternalBrokerClient) SetHandler(handler *Handler)\n```\nSetHandler sets the handler reference\n\n```go\nfunc (ebc *ExternalBrokerClient) Start(ctx context.Context) error\n```\nStart connects to the external MQTT broker\n\n```go\nfunc (ebc *ExternalBrokerClient) Stop(ctx context.Context) error\n```\nStop disconnects from the external broker\n\n```go\nfunc (ebc *ExternalBrokerClient) Subscribe(topicFilter string, qos byte, callback MessageCallback) error\n```\nSubscribe subscribes to a topic\n\n```go\nfunc (ebc *ExternalBrokerClient) Unsubscribe(topicFilter string) error\n```\nUnsubscribe unsubscribes from a topic\n\n```go\ntype ExternalBrokerConfig struct {\n\t// BrokerURL is the broker address (e.g., tcp://host:port or ssl://host:port)\n\tBrokerURL string\n\n\t// ClientID is a unique identifier for this handler instance\n\tClientID string\n\n\t// Username for MQTT authentication\n\tUsername string\n\n\t// Password for MQTT authentication\n\tPassword string\n\n\t// CleanSession flag (default: true)\n\tCleanSession bool\n\n\t// KeepAlive interval (default: 60s)\n\tKeepAlive time.Duration\n\n\t// ConnectTimeout for initial connection (default: 30s)\n\tConnectTimeout time.Duration\n\n\t// ReconnectDelay between reconnection attempts (default: 5s)\n\tReconnectDelay time.Duration\n\n\t// MaxReconnect attempts (0 = unlimited, default: 0)\n\tMaxReconnect int\n\n\t// TLSConfig for SSL/TLS connections\n\tTLSConfig *tls.Config\n}\n```\nExternalBrokerConfig for connecting as a client to external broker\n\n```go\ntype Handler struct {\n\t// contains filtered or unexported fields\n}\n```\nHandler handles MQTT messages and operations\n\n```go\nfunc NewHandler(db common.Database, registry common.ModelRegistry, config *Config) (*Handler, error)\n```\nNewHandler creates a new MQTT handler\n\n```go\nfunc NewHandlerWithBun(db *bun.DB, opts ...Option) (*Handler, error)\n```\nNewHandlerWithBun creates an MQTT handler with Bun database adapter\n\n```go\nfunc NewHandlerWithDatabase(db common.Database, registry common.ModelRegistry, opts ...Option) (*Handler, error)\n```\nNewHandlerWithDatabase creates an MQTT handler with a custom database adapter\n\n```go\nfunc NewHandlerWithGORM(db *gorm.DB, opts ...Option) (*Handler, error)\n```\nNewHandlerWithGORM creates an MQTT handler with GORM database adapter\n\n```go\nfunc (h *Handler) GetDatabase() common.Database\n```\nGetDatabase returns the database adapter\n\n```go\nfunc (h *Handler) GetRelationshipInfo(modelType reflect.Type, relationName string) *common.RelationshipInfo\n```\nGetRelationshipInfo is a placeholder for relationship detection\n\n```go\nfunc (h *Handler) Hooks() *HookRegistry\n```\nHooks returns the hook registry\n\n```go\nfunc (h *Handler) Registry() common.ModelRegistry\n```\nRegistry returns the model registry\n\n```go\nfunc (h *Handler) SetDisallowNulls(disallow bool)\n```\ncreate creates a new record SetDisallowNulls controls whether explicit null values in update payloads are ignored. By default a key present in the payload overwrites the stored value, including \"\" and null. When true, null values are skipped and the existing value is kept (\"\" still overwrites).\n\n```go\nfunc (h *Handler) Shutdown() error\n```\nShutdown gracefully shuts down the handler\n\n```go\nfunc (h *Handler) Start() error\n```\nStart initializes and starts the handler\n\n```go\ntype HookContext = websocketspec.HookContext\n```\nHookContext contains all context for hook execution Note: For MQTT, the Client is stored in Metadata\\[\"mqtt\\_client\"]\n\n```go\ntype HookFunc = websocketspec.HookFunc\n```\nHookFunc is a function that executes during a lifecycle hook\n\n```go\ntype HookRegistry = websocketspec.HookRegistry\n```\nHookRegistry manages all registered hooks\n\n```go\nfunc NewHookRegistry() *HookRegistry\n```\nNewHookRegistry creates a new hook registry\n\n```go\ntype HookType = websocketspec.HookType\n```\nHookType defines the type of lifecycle hook\n\n```go\ntype Message = websocketspec.Message\n```\nMessage represents an MQTT message (identical to WebSocket message protocol)\n\n```go\ntype MessageCallback func(topic string, payload []byte)\n```\nMessageCallback is called when a message arrives\n\n```go\ntype MessageType = websocketspec.MessageType\n```\nMessageType defines the type of message\n\n```go\ntype NotificationMessage = websocketspec.NotificationMessage\n```\nNotificationMessage is sent to subscribers when data changes\n\n```go\ntype OperationType = websocketspec.OperationType\n```\nOperationType defines the operation to perform\n\n```go\ntype Option func(*Handler) error\n```\nOption is a functional option for configuring the handler\n\n```go\nfunc WithEmbeddedBroker(config BrokerConfig) Option\n```\nWithEmbeddedBroker configures the handler to use an embedded MQTT broker\n\n```go\nfunc WithExternalBroker(config ExternalBrokerConfig) Option\n```\nWithExternalBroker configures the handler to connect to an external MQTT broker\n\n```go\nfunc WithHooks(hooks *HookRegistry) Option\n```\nWithHooks sets a pre-configured hook registry\n\n```go\nfunc WithQoS(request, response, notification byte) Option\n```\nWithQoS sets custom QoS levels for different message types\n\n```go\nfunc WithTopicPrefix(prefix string) Option\n```\nWithTopicPrefix sets a custom topic prefix (default: \"spec\")\n\n```go\ntype QoSConfig struct {\n\t// Request messages QoS (default: 1 - at-least-once)\n\tRequest byte\n\n\t// Response messages QoS (default: 1 - at-least-once)\n\tResponse byte\n\n\t// Notification messages QoS (default: 1 - at-least-once)\n\tNotification byte\n}\n```\nQoSConfig defines quality of service levels for different message types\n\n```go\ntype ResponseMessage = websocketspec.ResponseMessage\n```\nResponseMessage is sent back to clients after processing requests\n\n```go\ntype Subscription = websocketspec.Subscription\n```\nSubscription represents an active subscription to entity changes The key difference for MQTT: notifications are delivered via MQTT publish to spec/{client\\_id}/notify/{subscription\\_id} instead of WebSocket send\n\n```go\ntype SubscriptionManager = websocketspec.SubscriptionManager\n```\nSubscriptionManager manages all active subscriptions\n\n```go\nfunc NewSubscriptionManager() *SubscriptionManager\n```\nNewSubscriptionManager creates a new subscription manager\n\n```go\ntype TimeoutConfig struct {\n\t// Connect timeout for MQTT connection (default: 30s)\n\tConnect time.Duration\n\n\t// Publish timeout for publishing messages (default: 5s)\n\tPublish time.Duration\n\n\t// Disconnect timeout for graceful shutdown (default: 10s)\n\tDisconnect time.Duration\n}\n```\nTimeoutConfig defines timeouts for various operations\n\n```go\ntype TopicConfig struct {\n\t// Prefix for all topics (default: \"spec\")\n\t// Topics will be: {Prefix}/{client_id}/request|response|notify/{sub_id}\n\tPrefix string\n}\n```\nTopicConfig defines the MQTT topic structure\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/mqttspec","name":"mqttspec","synopsis":"","isRedistributable":true}
