nats

package module
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 4 Imported by: 0

README

axon-nats

Primitives · Part of the lamina workspace

NATS adapters for axon services. EventBus[T] implements sse.Publisher[T] from axon, enabling cross-instance fan-out for SSE services connected to a NATS cluster.

Getting started

go get github.com/benaskins/axon-nats
conn, _ := nats.Connect("nats://127.0.0.1:4222")
bus := axonnats.NewEventBus[MyEvent](conn, axonnats.WithSubject("chat.events"))

bus.Publish(MyEvent{Text: "hello"})

ch := bus.Subscribe("client-1")
ev := <-ch

bus.Unsubscribe("client-1")
bus.Close()

Key types

  • EventBus[T] — NATS-backed pub/sub implementing sse.Publisher[T]. Each subscriber gets a unique NATS subscription for fan-out delivery.
  • NewEventBus[T](conn, opts...) — Constructor taking a *nats.Conn and functional options.
  • WithSubject(subject) — Option to set the NATS subject (default: "events").

License

MIT

Documentation

Overview

Package nats provides NATS-backed adapters for axon services. EventBus implements distributed pub/sub, enabling cross-instance fan-out for SSE services connected to a NATS cluster.

Class: primitive UseWhen: Horizontal scaling, pub/sub.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

type EventBus[T any] struct {
	// contains filtered or unexported fields
}

EventBus is a distributed pub/sub backed by NATS, enabling horizontal scaling of SSE services. Events published on any instance are delivered to subscribers on all instances connected to the same NATS cluster.

EventBus implements push.Publisher[T].

func NewEventBus

func NewEventBus[T any](conn *nats.Conn, opts ...Option) *EventBus[T]

NewEventBus creates an EventBus connected to the given NATS connection. Each subscriber gets a unique NATS subscription on the configured subject, ensuring fan-out delivery across all instances.

func (*EventBus[T]) Close

func (b *EventBus[T]) Close()

Close drains all subscriptions and closes local channels.

func (*EventBus[T]) Publish

func (b *EventBus[T]) Publish(ev T)

func (*EventBus[T]) Subscribe

func (b *EventBus[T]) Subscribe(clientID string) <-chan T

func (*EventBus[T]) Unsubscribe

func (b *EventBus[T]) Unsubscribe(clientID string)

type Option

type Option func(*config)

Option configures an EventBus.

func WithSubject

func WithSubject(subject string) Option

WithSubject sets the NATS subject used for publishing and subscribing. Defaults to "events".

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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