Documentation
¶
Index ¶
- func Handler(m *Manager, opts ...HandlerOption) echo.HandlerFunc
- type Broadcaster
- func (b *Broadcaster) Publish(data any)
- func (b *Broadcaster) SetOnEmptyCallback(f func())
- func (b *Broadcaster) SetPollingService(service PollingService)
- func (b *Broadcaster) StartPolling()
- func (b *Broadcaster) StopPolling() error
- func (b *Broadcaster) Subscribe() SubscriberCh
- func (b *Broadcaster) Unsubscribe(ch SubscriberCh)
- type HandlerConfig
- type HandlerOption
- type Manager
- type Polling
- type PollingService
- type Publisher
- type SubscriberCh
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
func Handler(m *Manager, opts ...HandlerOption) echo.HandlerFunc
Handler creates an SSE handler that listens for messages on a specific topic and sends the data to the client. It can be configured with options such as a PollingService to periodically fetch data and publish it to subscribers.
Types ¶
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
Broadcaster manages a set of subscribers and broadcasts messages to them.
func NewBroadcaster ¶
func NewBroadcaster(lc log.Logger) *Broadcaster
NewBroadcaster creates a new instance of Broadcaster.
func (*Broadcaster) Publish ¶
func (b *Broadcaster) Publish(data any)
Publish sends data to all subscribers.
func (*Broadcaster) SetOnEmptyCallback ¶
func (b *Broadcaster) SetOnEmptyCallback(f func())
SetOnEmptyCallback sets a callback function that will be called when there are no subscribers left.
func (*Broadcaster) SetPollingService ¶
func (b *Broadcaster) SetPollingService(service PollingService)
SetPollingService sets the polling service for the broadcaster if auto-polling is required.
func (*Broadcaster) StartPolling ¶
func (b *Broadcaster) StartPolling()
StartPolling starts the polling service if it is set.
func (*Broadcaster) StopPolling ¶
func (b *Broadcaster) StopPolling() error
StopPolling stops the polling service if it is running. It cancels the polling context and stops the service.
func (*Broadcaster) Subscribe ¶
func (b *Broadcaster) Subscribe() SubscriberCh
Subscribe adds a new subscriber and returns a channel to receive messages.
func (*Broadcaster) Unsubscribe ¶
func (b *Broadcaster) Unsubscribe(ch SubscriberCh)
Unsubscribe should only be deferred after the subscription to ensure the channel will be closed properly.
type HandlerConfig ¶
type HandlerConfig struct {
PollingService PollingService
}
HandlerConfig holds the configuration for the SSE handler.
type HandlerOption ¶
type HandlerOption func(*HandlerConfig)
HandlerOption is a function that modifies the HandlerConfig.
func WithPollingService ¶
func WithPollingService(service PollingService) HandlerOption
WithPollingService returns a HandlerOption that sets the PollingService in the HandlerConfig.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages multiple broadcasters for different topics.
func NewManager ¶
NewManager creates a new SSE Manager instance.
func (*Manager) GetBroadcaster ¶
func (m *Manager) GetBroadcaster(topic string) (b *Broadcaster, isNew bool)
GetBroadcaster retrieves a broadcaster for the specified topic or creates a new one if it doesn't exist.
func (*Manager) RemoveBroadcaster ¶
RemoveBroadcaster removes a broadcaster for the specified topic.
type Polling ¶
type Polling struct {
// contains filtered or unexported fields
}
Polling is a struct that implements a polling mechanism for fetching data from a data source at regular intervals. It is designed to be started once and can be stopped gracefully.
func NewPolling ¶
func NewPolling(interval time.Duration, pollingFunc func(context.Context) (any, error), lc log.Logger) *Polling
NewPolling creates a new Polling instance with the specified interval and data source.
type PollingService ¶
PollingService is an interface for a service that periodically fetches data and publishes it to subscribers.
type Publisher ¶
type Publisher interface {
Publish(data any)
}
Publisher is an interface for publishing data to subscribers.
type SubscriberCh ¶
type SubscriberCh chan any
SubscriberCh is a channel type used for broadcasting messages.