Options
Emit Options
All buses accept functional options when emitting events:
bus.Emit(ctx, event, opts...)
EmitOptions Struct
type EmitOptions struct {
Subject string // defaults to evt.Name()
}
WithSubject
bus.Emit(ctx, myEvent, event.WithSubject("chat.message.room-general"))
Overrides the publish subject. If not set, the subject defaults to evt.Name(). This is useful for buses like JetStream that support hierarchical NATS subjects.
ApplyEmitOptions Helper
cfg := event.ApplyEmitOptions(evt, opts...)
// cfg.Subject is pre-seeded with evt.Name()
Subscribe Options
All buses accept functional options when subscribing:
bus.Subscribe(ctx, subject, handler, opts...)
SubscribeOptions Struct
type SubscribeOptions struct {
HandlerName string
Stream string
Consumer string
MaxDeliver int
BackOff []time.Duration
}
Available Options
WithHandlerName
event.WithHandlerName("send-welcome-email")
Identifies the handler for metrics, logging, and idempotency key scoping. If not set, defaults to the subject name.
WithStream
event.WithStream("CHAT_EVENTS")
Sets the JetStream stream name. Required for JetStream subscriptions.
WithConsumer
event.WithConsumer("chat-processor")
Sets the durable consumer name for JetStream. Defaults to the subject name if not set.
WithMaxDeliver
event.WithMaxDeliver(5)
Limits redelivery attempts per message in JetStream. If zero, messages are redelivered indefinitely (a warning is logged).
WithBackOff
event.WithBackOff([]time.Duration{
1 * time.Second,
5 * time.Second,
30 * time.Second,
1 * time.Minute,
})
Sets delays between JetStream redelivery attempts. If nil, a default progressive backoff is used.
ApplyOptions Helper
cfg := event.ApplyOptions(opts...)
Applies all options and returns the resulting SubscribeOptions struct.