nats

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 7 Imported by: 0

README

Modulex NATS EventBus Adapter

This package provides a modulex.EventBus implementation backed by NATS.

Usage

import (
    "github.com/nats-io/nats.go"
    natsadapter "github.com/mediusfy/modulex/nats"
)

conn, err := nats.Connect(nats.DefaultURL)
if err != nil {
    return err
}
defer conn.Close()

eb := natsadapter.NewEventBus(conn)
manager, err := modulex.NewManager(eb, logger, nil)
if err != nil {
    return err
}

Behavior

  • Publish maps directly to conn.Publish(topic, payload).
  • Subscribe creates a NATS subscription and adapts incoming messages to the generic modulex.EventHandler signature.
  • Close unsubscribes all registered subscriptions.

Testing

The adapter tests start an embedded NATS server using github.com/nats-io/nats-server/v2/test. Run them with:

go test ./nats/...

Documentation

Overview

Package nats provides a Modulex EventBus adapter backed by NATS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

type EventBus struct {
	// contains filtered or unexported fields
}

EventBus implements modulex.EventBus by wrapping a concrete NATS connection.

func NewEventBus

func NewEventBus(conn *nats.Conn) *EventBus

NewEventBus instantiates the NATS event bus driver.

The EventBus does not take ownership of conn: the caller creates and closes the underlying *nats.Conn, typically after modulex.Manager.StopModules has closed the EventBus. This lets a single connection be shared across multiple concerns outside the module lifecycle if desired.

func (*EventBus) Close

func (n *EventBus) Close(ctx context.Context) error

Close implements modulex.EventBus. It unsubscribes all registered NATS subscriptions but does not close the underlying *nats.Conn, which the caller owns.

func (*EventBus) Publish

func (n *EventBus) Publish(ctx context.Context, topic string, payload []byte) error

Publish implements modulex.EventBus.

func (*EventBus) Subscribe

func (n *EventBus) Subscribe(ctx context.Context, topic string, handler modulex.EventHandler) error

Subscribe implements modulex.EventBus. It registers a NATS subscription, adapting the incoming message to the generic EventHandler signature.

The subscriber's context is propagated into the handler. If the incoming NATS message carries W3C trace context headers, they are extracted and merged so OpenTelemetry span continuity is preserved across the broker.

Jump to

Keyboard shortcuts

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