Documentation
¶
Index ¶
- func ConstructSSETopic(c echo.Context) string
- 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 PollingConfig
- type PollingOption
- type PollingService
- type Publisher
- type Subscriber
- type SubscriberCh
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConstructSSETopic ¶ added in v2.0.3
func ConstructSSETopic(c echo.Context) string
ConstructSSETopic constructs a unique topic string based on the request context.
e.g. "/api/v3/device/all/sse?offset=10&labels=label1,label2"
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 CustomTopic string }
HandlerConfig holds the configuration for the SSE handler.
type HandlerOption ¶
type HandlerOption func(*HandlerConfig)
HandlerOption is a function that modifies the HandlerConfig.
func WithCustomTopic ¶ added in v2.0.5
func WithCustomTopic(topic string) HandlerOption
WithCustomTopic returns a HandlerOption that sets a custom topic in 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) CreateOrGetBroadcaster ¶ added in v2.0.3
func (m *Manager) CreateOrGetBroadcaster(topic string) (b *Broadcaster, isNew bool)
CreateOrGetBroadcaster retrieves a broadcaster for the specified topic or creates a new one if it doesn't exist.
func (*Manager) GetBroadcaster ¶
func (m *Manager) GetBroadcaster(topic string) (b *Broadcaster, ok bool)
GetBroadcaster retrieves a broadcaster for the specified topic.
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(lc log.Logger, pollingFunc func(context.Context) (any, error), opts ...PollingOption) *Polling
NewPolling creates a new Polling instance with the specified interval and data source.
type PollingConfig ¶ added in v2.0.8
type PollingConfig struct { ApiVersion string // contains filtered or unexported fields }
type PollingOption ¶ added in v2.0.8
type PollingOption func(*PollingConfig)
PollingOption is a function that modifies the PollingConfig.
func WithCustomApiVersion ¶ added in v2.0.8
func WithCustomApiVersion(apiVersion string) PollingOption
WithCustomApiVersion returns a PollingOption that sets a custom API version in the PollingConfig, which is used to present the API version for the error response when polling fails. Default is common.ApiVersion set in go-mod-edge-utils if not set.
func WithCustomPollingInterval ¶ added in v2.0.8
func WithCustomPollingInterval(interval time.Duration) PollingOption
WithCustomPollingInterval returns a PollingOption that sets a custom polling interval in the PollingConfig. Default is 5 seconds if not set.
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 Subscriber ¶ added in v2.0.4
type Subscriber struct {
// contains filtered or unexported fields
}
type SubscriberCh ¶
type SubscriberCh chan any
SubscriberCh is a channel type used for broadcasting messages.