sse

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sse provides a server implementation for Server-Sent Events (SSE). SSE is a technology enabling a client to receive automatic updates from a server via HTTP connection.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientClosed = errors.New("sse: client closed")
)
View Source
var ErrNotStreamer = errors.New("sse: not streamer")

Functions

This section is empty.

Types

type Conn added in v1.1.4

type Conn struct {
	sync.Mutex
	ID string
	// contains filtered or unexported fields
}

Conn represents a connection to a streaming service. It holds the client's ID, a Streamer instance for managing the stream, a context for cancellation and timeout, and a channel for signaling closure.

func (*Conn) Close added in v1.1.4

func (c *Conn) Close()

Close gracefully shuts down the Client by sending a signal to the close channel. This method should be called to ensure that any ongoing operations are properly terminated.

func (*Conn) Connect added in v1.1.4

func (c *Conn) Connect(ctx context.Context, s Streamer)

Connect establishes a connection for the Client using the provided Streamer. It assigns the Streamer to the Client's rw field and ensures that it implements the http.Flusher interface for flushing data.

func (*Conn) Send added in v1.1.4

func (c *Conn) Send(evt Event) error

Send sends an event to the client by writing the event name and data to the response writer. It marshals the event data into JSON format and flushes the output to ensure the data is sent immediately. This method is part of the Client struct and is intended for use in server-sent events (SSE) communication.

func (*Conn) Wait added in v1.1.4

func (c *Conn) Wait()

Wait blocks until the context is done or the client is closed. It listens for either the cancellation of the context or a signal to close the client, allowing for graceful shutdown.

type Error

type Error struct {
	ClientID string
	// contains filtered or unexported fields
}

func NewError

func NewError(clientID string, err error) *Error

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Event

type Event interface {
	Write(r io.Writer) error
}

Event represents an interface for writing event data to an io.Writer. Implementations of this interface must provide the Write method, which takes an io.Writer and returns an error if the write operation fails.

type EventReader added in v1.1.4

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

func NewReader added in v1.1.4

func NewReader(r io.ReadCloser) *EventReader

func (*EventReader) Close added in v1.1.4

func (r *EventReader) Close() error

func (*EventReader) Next added in v1.1.4

func (r *EventReader) Next() (TextEvent, error)

type JsonEvent

type JsonEvent struct {
	ID    string
	Name  string
	Retry int
	Data  any
}

JsonEvent represents an event with a name and associated data. It can be used to structure events in a JSON format in the SSE (Server-Sent Events) protocol.

func (*JsonEvent) Write

func (e *JsonEvent) Write(w io.Writer) error

Write serializes the JsonEvent to the provided io.Writer in the SSE format. It writes the event name and the JSON-encoded data, followed by a double newline to indicate the end of the event. If an error occurs during marshaling or writing, it returns the error.

type PingEvent added in v1.1.4

type PingEvent struct {
}

func (*PingEvent) Write added in v1.1.4

func (evt *PingEvent) Write(w io.Writer) error

type Server

type Server struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Server represents a structure that manages connected clients in a concurrent environment. It uses a read-write mutex to ensure safe access to the clients map, which holds the active Client instances identified by their unique keys.

func New

func New() *Server

New creates and returns a new instance of the Server struct.

func (*Server) Broadcast

func (s *Server) Broadcast(ctx context.Context, event Event) ([]error, error)

Broadcast sends the specified event to all connected clients. It acquires a read lock to ensure thread-safe access to the clients slice, and spawns a goroutine for each client to handle the sending of the event.

func (*Server) Get

func (s *Server) Get(id string) *Conn

Get retrieves the Client associated with the given id from the Server. It uses a read lock to ensure thread-safe access to the clients map. Returns nil if no Client is found for the specified id.

func (*Server) Join

func (s *Server) Join(ctx context.Context, id string, rw http.ResponseWriter) (*Conn, error)

Join adds a new client to the server or retrieves an existing one based on the provided ID. It establishes a connection with the specified Streamer and sets the appropriate headers for Server-Sent Events (SSE). If a client with the given ID already exists, it reuses that client.

func (*Server) Leave

func (s *Server) Leave(id string)

Leave removes a client from the server's client list by its ID. This method is safe for concurrent use, as it locks the server before modifying the clients map and ensures that the lock is released afterward.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown gracefully closes all active client connections and cleans up the client list. It locks the server to ensure thread safety during the shutdown process.

type Streamer

type Streamer interface {
	http.ResponseWriter
	http.Flusher
}

func NewStreamer

func NewStreamer(rw http.ResponseWriter) (Streamer, error)

NewStreamer creates a new Streamer instance from the provided http.ResponseWriter. It returns an error if the ResponseWriter is nil or does not implement the http.Flusher interface. This function is intended for use in handling server-sent events (SSE).

type TextEvent

type TextEvent struct {
	ID    string
	Name  string
	Retry int
	Data  string
}

TextEvent represents a simple event structure with a name and associated data. It is used to encapsulate information for events in the SSE (Server-Sent Events) protocol.

func (*TextEvent) Write

func (e *TextEvent) Write(w io.Writer) error

Write formats the TextEvent as a string and writes it to the provided io.Writer. It outputs the event name and data in the SSE format, followed by two newlines. Returns an error if the write operation fails.

Jump to

Keyboard shortcuts

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