nats

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithoutJetstream

func WithoutJetstream(nc *NatsConnection)

Types

type CommandPayload added in v0.33.0

type CommandPayload struct {
	Method  string            `json:"method"`
	Headers map[string]string `json:"headers"`
	Query   map[string]string `json:"query"`
	Body    []byte            `json:"body"`
}

CommandPayload is the NATS message published by the command HTTP endpoint. It carries the full HTTP context so subscribers can act on method, headers, query parameters, and body without needing HTTP access.

func (CommandPayload) BodyBytes added in v0.33.0

func (p CommandPayload) BodyBytes() []byte

BodyBytes returns the raw request body bytes.

func (CommandPayload) BodyJSON added in v0.33.0

func (p CommandPayload) BodyJSON(v any) error

BodyJSON unmarshals the request body as JSON into v.

func (CommandPayload) BodyString added in v0.33.0

func (p CommandPayload) BodyString() string

BodyString returns the request body decoded as a UTF-8 string.

type NatsConnection

type NatsConnection struct {
	HostName     string
	Port         string
	UseJetstream bool

	Conn *nats.Conn
	JS   jetstream.JetStream
	// contains filtered or unexported fields
}

func DefaultNatsConnection

func DefaultNatsConnection() *NatsConnection

func New

func New(optFuncs ...NatsConnectionOption) *NatsConnection

func (*NatsConnection) Close

func (n *NatsConnection) Close()

func (*NatsConnection) Connect

func (n *NatsConnection) Connect() error

func (*NatsConnection) Publish

func (n *NatsConnection) Publish(ctx context.Context, subject string, payload []byte, opts ...jetstream.PublishOpt)

func (*NatsConnection) Stream

func (n *NatsConnection) Stream(ctx context.Context, subject string) (stream jetstream.Stream, consumer jetstream.Consumer, err error)

func (*NatsConnection) Subscribe

func (n *NatsConnection) Subscribe(subject string) (sub *nats.Subscription, ch chan *nats.Msg, err error)

func (*NatsConnection) SubscribeAttributes added in v0.33.0

func (n *NatsConnection) SubscribeAttributes(entityId string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeAttributes subscribes to attribute change events for the given entity. The handler receives the full event (old and new state).

func (*NatsConnection) SubscribeCommand added in v0.33.0

func (n *NatsConnection) SubscribeCommand(commandName string, handler func(CommandPayload) error) (*nats.Subscription, error)

SubscribeCommand subscribes to command events for the given command name. The handler receives the full CommandPayload including HTTP method, headers, query, and body.

func (*NatsConnection) SubscribeNFC added in v0.33.0

func (n *NatsConnection) SubscribeNFC(tagId string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeNFC subscribes to NFC tag scan events for the given tag ID.

func (*NatsConnection) SubscribeSchedule added in v0.33.0

func (n *NatsConnection) SubscribeSchedule(scheduleName string, handler func() error) (*nats.Subscription, error)

SubscribeSchedule subscribes to schedule fire events for the given schedule name.

func (*NatsConnection) SubscribeState added in v0.33.0

func (n *NatsConnection) SubscribeState(entityId string, handler func(ha.StateData) error) (*nats.Subscription, error)

SubscribeState subscribes to state change events for the given entity. The handler receives only the new state.

func (*NatsConnection) SubscribeStateEvent added in v0.33.0

func (n *NatsConnection) SubscribeStateEvent(entityId string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeStateEvent subscribes to state change events for the given entity. The handler receives the full EventData including both old and new state.

func (*NatsConnection) SubscribeTimer added in v0.33.0

func (n *NatsConnection) SubscribeTimer(timerName string, handler func() error) (*nats.Subscription, error)

SubscribeTimer subscribes to timer completion events for the given timer name.

func (*NatsConnection) SubscribeZHA added in v0.33.0

func (n *NatsConnection) SubscribeZHA(deviceIeee string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeZHA subscribes to ZHA device events for the given IEEE address.

func (*NatsConnection) SubscribeZwave added in v0.33.0

func (n *NatsConnection) SubscribeZwave(deviceId string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeZwave subscribes to Z-Wave device events for the given device ID.

func (*NatsConnection) SubscribeZwaveScene added in v0.33.0

func (n *NatsConnection) SubscribeZwaveScene(deviceId string, handler func(ha.EventData) error) (*nats.Subscription, error)

SubscribeZwaveScene subscribes to Z-Wave scene events for the given device ID.

type NatsConnectionOption

type NatsConnectionOption func(*NatsConnection)

func WithClientName

func WithClientName(name string) NatsConnectionOption

func WithConnectionOption

func WithConnectionOption(opt nats.Option) NatsConnectionOption

func WithHostName

func WithHostName(hostname string) NatsConnectionOption

func WithJetstreamnOption

func WithJetstreamnOption(opt jetstream.JetStreamOpt) NatsConnectionOption

func WithLogger

func WithLogger(logger *slog.Logger) NatsConnectionOption

func WithPort

func WithPort(port string) NatsConnectionOption

func WithToken

func WithToken(token string) NatsConnectionOption

type TopicType

type TopicType string
const AttributeTopicType TopicType = "attributes"
const CommandTopicType TopicType = "command"
const NfcTopicType TopicType = "nfc"
const ScheduleTopicType TopicType = "schedules"
const StateTopicType TopicType = "states"
const TimerTopicType TopicType = "timers"
const ZhaTopicType TopicType = "zha"
const ZwaveSceneTopicType TopicType = "zwave-scene"
const ZwaveTopicType TopicType = "zwave"

func (TopicType) GetTopic

func (t TopicType) GetTopic(id string) string

Jump to

Keyboard shortcuts

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