natsjs

package
v0.0.0-...-0b0e567 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

NATS JetStream

This plugin uses NATS with JetStream to send and receive events.

Create a stream

ev, err := natsjs.NewStream(
  natsjs.Address("nats://10.0.1.46:4222"),
  natsjs.MaxAge(24*160*time.Minute),
)

Consume a stream

ee, err := events.Consume("test",
  events.WithAutoAck(false, time.Second*30),
  events.WithGroup("testgroup"),
)
if err != nil {
  panic(err)
}
go func() {
  for {
    msg := <-ee
    // Process the message
    logger.Info("Received message:", string(msg.Payload))
    err := msg.Ack()
    if err != nil {
      logger.Error("Error acknowledging message:", err)
    } else {
      logger.Info("Message acknowledged")
    }
  }
}()

Publish an Event to the stream

err = ev.Publish("test", []byte("hello world"))
if err != nil {
  panic(err)
}

Documentation

Overview

Package natsjs provides a NATS Jetstream implementation of the events.Stream interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStream

func NewStream(opts ...Option) (events.Stream, error)

NewStream returns an initialized nats stream or an error if the connection to the nats server could not be established.

Types

type Option

type Option func(o *Options)

Option is a function which configures options.

func Address

func Address(addr string) Option

Address of the nats cluster.

func Authenticate

func Authenticate(username, password string) Option

Authenticate authenticates the connection with the given username and password.

func ClientID

func ClientID(id string) Option

ClientID sets the client id for the nats connection.

func ClusterID

func ClusterID(id string) Option

ClusterID sets the cluster id for the nats connection.

func DisableDurableStreams

func DisableDurableStreams() Option

DisableDurableStreams will disable durable streams.

func Logger

func Logger(log logger.Logger) Option

Logger sets the underlying logger.

func MaxAge

func MaxAge(age time.Duration) Option

func MaxMsgSize

func MaxMsgSize(size int) Option

func Name

func Name(name string) Option

Name allows to add a name to the natsjs connection.

func NkeyConfig

func NkeyConfig(nkey string) Option

NkeyConfig string to use when connecting to the cluster.

func RetentionPolicy

func RetentionPolicy(rp int) Option

func SynchronousPublish

func SynchronousPublish(sync bool) Option

SynchronousPublish allows using a synchronous publishing instead of the default asynchronous.

func TLSConfig

func TLSConfig(t *tls.Config) Option

TLSConfig to use when connecting to the cluster.

type Options

type Options struct {
	ClusterID             string
	ClientID              string
	Address               string
	NkeyConfig            string
	TLSConfig             *tls.Config
	Logger                logger.Logger
	SyncPublish           bool
	Name                  string
	DisableDurableStreams bool
	Username              string
	Password              string
	RetentionPolicy       int
	MaxAge                time.Duration
	MaxMsgSize            int
}

Options which are used to configure the nats stream.

Source Files

  • nats.go
  • options.go

Jump to

Keyboard shortcuts

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