40 lines
1.2 KiB
Go
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
|
|
}
|