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 ¶
var (
ErrClientClosed = errors.New("sse: client closed")
)
var ErrNotStreamer = errors.New("sse: not streamer")
Functions ¶
This section is empty.
Types ¶
type Conn ¶ added in v1.1.4
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
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
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.
type Event ¶
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 ¶
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.
type Server ¶
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 (*Server) Broadcast ¶
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 ¶
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 ¶
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.
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).