sse

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: Apache-2.0 Imports: 11 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 HandleSSE added in v2.0.3

func HandleSSE(c echo.Context, serviceCtx context.Context, b *Broadcaster, heartbeatInterval time.Duration) error

HandleSSE accepts an echo.Context and a Broadcaster (created by users), provides a more flexible way to handle Server-Sent Events (SSE) compared to the Handler function. e.g., The users want to define their own SSE topics and use the broadcaster to publish messages to subscribers manually.

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

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(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.

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 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