persona-community-2/pkg/realtime/publisher.go
jordan cb3d4d5786
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/manual/woodpecker Pipeline was successful
Initialize project from skeleton template
2026-02-23 10:53:55 +00:00

40 lines
1.2 KiB
Go

package realtime
// EventPublisher sends SSE events to users and channels.
// Implementations:
// - SSEPublisher: publishes via Redis for cross-process delivery (production)
// - LocalPublisher: delivers directly to SSEHub in-process (development)
type EventPublisher interface {
SendToUser(userID string, event *SSEEvent) error
SendToChannel(channel string, event *SSEEvent) error
}
// Compile-time interface checks.
var (
_ EventPublisher = (*SSEPublisher)(nil)
_ EventPublisher = (*LocalPublisher)(nil)
)
// LocalPublisher delivers SSE events directly to the local SSEHub.
// Use this when the service and worker run in the same process (standalone/dev mode).
type LocalPublisher struct {
hub *SSEHub
}
// NewLocalPublisher creates a publisher that sends events directly to the SSEHub.
func NewLocalPublisher(hub *SSEHub) *LocalPublisher {
return &LocalPublisher{hub: hub}
}
// SendToUser sends an event to a user's SSE channel.
func (p *LocalPublisher) SendToUser(userID string, event *SSEEvent) error {
p.hub.SendToUser(userID, event)
return nil
}
// SendToChannel sends an event to a named SSE channel.
func (p *LocalPublisher) SendToChannel(channel string, event *SSEEvent) error {
p.hub.SendToChannel(channel, event)
return nil
}