sse

package
v2.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

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

func NewManager(ctx context.Context, lc log.Logger, heartbeatInterval time.Duration) *Manager

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

func (m *Manager) RemoveBroadcaster(topic string)

RemoveBroadcaster removes a broadcaster for the specified topic.

func (*Manager) Shutdown added in v2.0.4

func (m *Manager) Shutdown()

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.

func (*Polling) Start

func (p *Polling) Start(publisher Publisher)

Start initializes the polling mechanism. It sets up an internal context with cancel and starts the polling goroutine.

func (*Polling) Stop

func (p *Polling) Stop() error

Stop gracefully stops the polling mechanism. It cancels the context and waits for the polling goroutine to finish.

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

type PollingService interface {
	Start(publisher Publisher)
	Stop() error
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL