feat: implement cross-node event broadcasting with NATSEventBus
All checks were successful
CI / build (pull_request) Successful in 20s

- Add UpdateVersionCache method to JetStreamEventStore for cache synchronization
- Add SubscribeToEventStored convenience helper to NATSEventBus
- Create integration tests for cross-node broadcasting scenarios
- Add example demonstrating NATSEventBus + JetStreamEventStore integration
This commit is contained in:
Hugo Nijhuis
2026-05-17 14:03:48 +02:00
parent 6041479286
commit 5c01911e3c
6 changed files with 810 additions and 0 deletions

View File

@@ -210,6 +210,26 @@ func (neb *NATSEventBus) Publish(namespaceID string, event *Event) {
}
}
// SubscribeToEventStored creates a subscription specifically for EventStored events.
// This is a convenience method for the common pattern of listening to persisted events
// to update version cache or trigger other actions.
//
// Example:
//
// eventStoredCh := natsBus.SubscribeToEventStored("tenant-abc")
// go func() {
// for event := range eventStoredCh {
// actorID := event.Data["actorId"].(string)
// version := int64(event.Data["version"].(float64))
// store.UpdateVersionCache(actorID, version)
// }
// }()
func (neb *NATSEventBus) SubscribeToEventStored(namespacePattern string) <-chan *Event {
return neb.SubscribeWithFilter(namespacePattern, &SubscriptionFilter{
EventTypes: []string{EventTypeEventStored},
})
}
// Stop closes the NATS event bus and all subscriptions
func (neb *NATSEventBus) Stop() {
neb.mutex.Lock()