sse

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

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

func NewManager(lc log.Logger) *Manager

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

func (m *Manager) RemoveBroadcaster(topic string)

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.

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