package pubsub import "context" // Event represents a real-time event to be published. type Event struct { Type string `json:"type"` // message_created, conversation_updated, etc AccountID uint `json:"account_id"` Payload map[string]interface{} `json:"payload"` } // PubSub defines the interface for publishing and subscribing to real-time events. // Implementations can use Redis pub/sub, NATS, or in-process channels. type PubSub interface { // Publish sends an event to all subscribers of the given topic. Publish(ctx context.Context, topic string, event Event) error // Subscribe registers a handler for events on the given topic. Subscribe(ctx context.Context, topic string, handler EventHandler) error // Unsubscribe removes a handler from the given topic. Unsubscribe(ctx context.Context, topic string) error } // EventHandler is a callback function that processes received events. type EventHandler func(event Event) // InMemoryPubSub is a simple in-process pub/sub for development/testing. type InMemoryPubSub struct { subscribers map[string][]EventHandler } // NewInMemoryPubSub creates a new in-memory pub/sub instance. func NewInMemoryPubSub() *InMemoryPubSub { return &InMemoryPubSub{ subscribers: make(map[string][]EventHandler), } } func (ps *InMemoryPubSub) Publish(ctx context.Context, topic string, event Event) error { handlers, ok := ps.subscribers[topic] if !ok { return nil } for _, handler := range handlers { handler(event) } return nil } func (ps *InMemoryPubSub) Subscribe(ctx context.Context, topic string, handler EventHandler) error { ps.subscribers[topic] = append(ps.subscribers[topic], handler) return nil } func (ps *InMemoryPubSub) Unsubscribe(ctx context.Context, topic string) error { delete(ps.subscribers, topic) return nil }