sse

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 10 Imported by: 0

README

sup/sse

Go Reference Test License

sup/sse is a high-reliability Server-Sent Events (SSE) client implementation for Go, built on top of the sup actor library. It provides thread-safe, supervised, and observable way to interact with Server-Sent Events (SSE) streams.

Features

  • Actor-Based Concurrency: Thread-safe access to hardware. Multiple goroutines can call the actor safely; the actor handles the queue.
  • Supervised Lifecycle: Designed to run under a sup.Supervisor. If the connection drops, the actor returns a fatal error, allowing the supervisor to handle reconnection.
  • Last-Event-ID: Automatically tracks the last event ID and includes it in the Last-Event-ID header for reconnections.

Quick start

package main

import (
    "context"
    "fmt"
    "time"

		"github.com/webermarci/sup"
    "github.com/webermarci/sup/sse"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	
	handler := func(sse.Event event) {
		fmt.Println(event)
	}
	
	actor := sse.NewActor("actor", "http://localhost:8080", handler,
		sse.WithTimeout(10 * time.Second),
	)

	supervisor := sup.NewSupervisor("root",
		sup.WithActor(actor),
		sup.WithPolicy(sup.Permanent),
		sup.WithRestartDelay(time.Second),
		sup.WithRestartLimit(5, 10 * time.Second),
	)
	
	supervisor.Run(ctx)
	supervisor.Wait()
}

Using it with pubsub

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/webermarci/pubsub"
    "github.com/webermarci/sup"
    "github.com/webermarci/sup/sse"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	pubsub := pubsub.New[string, sse.Event](10)

	handler := func(sse.Event event) {
		pubsub.Publish("sse", event)
	}
  
	actor := sse.NewActor("actor", "http://localhost:8080", handler,
		sse.WithTimeout(10 * time.Second),
	)

	supervisor := sup.NewSupervisor("root",
		sup.WithActor(actor),
		sup.WithPolicy(sup.Permanent),
		sup.WithRestartDelay(time.Second),
		sup.WithRestartLimit(5, 10 * time.Second),
	)
  
	go supervisor.Run(ctx)
  
	events := pubsub.Subscribe(ctx, "sse")
  
	go func() {
		for event := range events {
			fmt.Println(event)
		}
	}()
  
	supervisor.Wait()
	pubsub.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

type Actor struct {
	*sup.BaseActor
	// contains filtered or unexported fields
}

Actor is responsible for connecting to an SSE endpoint, reading and parsing incoming events, and invoking a handler function for each event.

func NewActor

func NewActor(name string, url string, onEvent func(Event), opts ...ActorOption) *Actor

NewActor creates a new Actor with the specified URL, event handler, and optional configuration options.

func (*Actor) LastEventID

func (a *Actor) LastEventID() string

LastEventID returns the ID of the last successfully received event.

func (*Actor) Run

func (a *Actor) Run(ctx context.Context) (err error)

Run establishes a connection to the SSE endpoint and processes incoming events until the context is canceled or an error occurs.

type ActorOption

type ActorOption func(*Actor)

ActorOption defines a function type for configuring an Actor.

func WithHTTPClient

func WithHTTPClient(c *http.Client) ActorOption

WithHTTPClient allows the caller to provide a custom http.Client for making requests to the SSE endpoint.

func WithLastEventID

func WithLastEventID(id string) ActorOption

WithLastEventID sets the initial Last-Event-ID to be used when connecting.

func WithOnConnect

func WithOnConnect(handler func(url string, lastID string)) ActorOption

WithOnConnect allows the caller to provide a callback function that will be invoked when the Actor successfully connects to the SSE endpoint.

func WithOnError

func WithOnError(handler func(error)) ActorOption

WithOnError allows the caller to provide a callback function that will be invoked whenever an error occurs during the Actor's operation.

func WithTimeout

func WithTimeout(d time.Duration) ActorOption

WithTimeout sets the duration after which the SSE connection will be considered timed out if no events are received.

type Event

type Event struct {
	ID   string
	Data string
	Name string
}

Event represents a single SSE event with its ID, data, and optional name.

Jump to

Keyboard shortcuts

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