events

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package events provides a simple Server-Sent Events (SSE) broker for real-time event streaming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatSSE

func FormatSSE(event Event) string

FormatSSE formats an event for Server-Sent Events protocol.

Types

type BroadcastEvent

type BroadcastEvent struct {
	Topic string
	Event Event
}

BroadcastEvent represents an event to be broadcasted to clients.

type BroadcastPayload

type BroadcastPayload struct {
	Topic string                 `json:"topic"`
	Type  string                 `json:"type"`
	Data  map[string]interface{} `json:"data"`
}

BroadcastPayload represents the JSON payload for broadcasting events.

type Broker

type Broker struct {
	// contains filtered or unexported fields
}

Broker manages SSE connections and event broadcasting.

func NewBroker

func NewBroker() *Broker

NewBroker creates a new SSE broker.

func (*Broker) Broadcast

func (b *Broker) Broadcast(topic string, eventType string, data map[string]interface{})

Broadcast sends an event to all subscribed clients.

func (*Broker) GetClientCount

func (b *Broker) GetClientCount() int

GetClientCount returns the number of connected clients.

func (*Broker) Register

func (b *Broker) Register(client *Client)

Register registers a new client with the broker.

func (*Broker) Start

func (b *Broker) Start()

Start starts the broker's event loop.

func (*Broker) Stop

func (b *Broker) Stop()

Stop stops the broker and closes all client connections.

func (*Broker) Unregister

func (b *Broker) Unregister(client *Client)

Unregister removes a client from the broker.

type Client

type Client struct {
	ID      string
	Channel chan Event
	Topics  map[string]bool // Topics this client is subscribed to
	// contains filtered or unexported fields
}

Client represents a connected SSE client.

func NewClient

func NewClient(id string) *Client

NewClient creates a new SSE client.

func (*Client) IsSubscribed

func (c *Client) IsSubscribed(topic string) bool

IsSubscribed checks if client is subscribed to a topic.

func (*Client) Subscribe

func (c *Client) Subscribe(topic string)

Subscribe adds a topic to this client's subscriptions.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(topic string)

Unsubscribe removes a topic from this client's subscriptions.

type Event

type Event struct {
	Type      string                 `json:"type"`
	Data      map[string]interface{} `json:"data"`
	Timestamp time.Time              `json:"timestamp"`
}

Event represents a single event to be broadcasted.

type PublishConfig

type PublishConfig struct {
	GatewayURL  string
	ServiceName string
	Timeout     time.Duration
}

PublishConfig configures the event publisher.

type Publisher

type Publisher struct {
	// contains filtered or unexported fields
}

Publisher publishes events to the Gateway's SSE broker.

func NewPublisher

func NewPublisher(config PublishConfig) *Publisher

NewPublisher creates a new event publisher.

func (*Publisher) PublishEvent

func (p *Publisher) PublishEvent(topic, eventType string, data map[string]interface{}) error

PublishEvent publishes an event to the SSE broker via the Gateway. Topic determines which subscribers receive the event. EventType is the event name (e.g., "transaction.created"). Data contains the event payload.

func (*Publisher) PublishEventAsync

func (p *Publisher) PublishEventAsync(topic, eventType string, data map[string]interface{})

PublishEventAsync publishes an event asynchronously (fire and forget). Errors are logged but not returned.

func (*Publisher) PublishRiskEvent

func (p *Publisher) PublishRiskEvent(eventType string, data map[string]interface{})

PublishRiskEvent publishes a risk-related event.

func (*Publisher) PublishTransactionEvent

func (p *Publisher) PublishTransactionEvent(eventType string, transactionID string, data map[string]interface{})

PublishTransactionEvent publishes a transaction-related event.

func (*Publisher) PublishUserEvent

func (p *Publisher) PublishUserEvent(eventType string, userID string, data map[string]interface{})

PublishUserEvent publishes a user-related event.

func (*Publisher) PublishWalletEvent

func (p *Publisher) PublishWalletEvent(eventType string, walletID string, data map[string]interface{})

PublishWalletEvent publishes a wallet-related event.

Jump to

Keyboard shortcuts

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